在使用正则表达式匹配较长字符串时出现StackOverflowError – 优化

huangapple go评论101阅读模式
英文:

StackOverflowError when matching longer string using Regex - Optimization

问题

我正在使用正则表达式来匹配传入的文件内容,以检测具有以下模式的 ID:

  1. AXXXXXXXXXX-MID-XX(其中 X = 长度为 10 2 的数字值)

这是我的正则表达式:(.|\n|\r)*(A[0-9]{10}-MID-[0-9]{2})(.|\n|\r)*

但是,当内容超过 1500 个字符时,我会收到堆栈溢出错误。

寻求帮助,看看这是否可以进行优化?

这是 Java 代码 -

  1. String pattern1="(.|\n|\r)*(A[0-9]{10}-MID-[0-9]{2})(.|\n|\r)*";
  2. if(file_content.matches(pattern1)) {
  3. //...做一些操作 <-- 代码永远不会执行到这里。
  4. }

在使用正则表达式匹配较长字符串时出现StackOverflowError – 优化

英文:

I am using Regex for matching incoming file content to detect an ID which has following pattern

  1. AXXXXXXXXXX-MID-XX (Where X = numeric values with length 10 and 2)

Here's my Regex (.|\n|\r)*(A[0-9]{10}-MID-[0-9]{2})(.|\n|\r)*

But, when the content exceeds like 1500 characters, I get StackOverflow error.

在使用正则表达式匹配较长字符串时出现StackOverflowError – 优化
Seeking help here to check if this look like something which can be optimized?

Here's the Java Code -

  1. String pattern1=&quot;(.|\n|\r)*(A[0-9]{10}-MID-[0-9]{2})(.|\n|\r)*&quot;;
  2. if(file_content.matches(pattern1)) {
  3. //...Do something &lt;-- The code never reaches here.
  4. }

答案1

得分: 0

  1. 你不必要地使用了 `(.|\n|\r)*`以下是更清晰更高效的方法
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Pattern pattern = Pattern.compile("A[0-9]{10}-MID-[0-9]{2}");
  7. String fileContent = "A1234567890-MID-12"; // 将其替换为实际内容
  8. Matcher matcher = pattern.matcher(fileContent);
  9. while (matcher.find()) {
  10. String id = matcher.group(); // 如果你想获取ID
  11. System.out.println(id);
  12. // ...做点什么
  13. }
  14. }
  15. }
  16. **输出:**
  17. A1234567890-MID-12
英文:

You have unnecessarily used (.|\n|\r)*. Given below is a cleaner and more performant way of doing it:

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Pattern pattern = Pattern.compile(&quot;A[0-9]{10}-MID-[0-9]{2}&quot;);
  6. String fileContent = &quot;A1234567890-MID-12&quot;;// Replace it with actual content
  7. Matcher matcher = pattern.matcher(fileContent);
  8. while (matcher.find()) {
  9. String id = matcher.group();// If you want to grab the ID
  10. System.out.println(id);
  11. // ...do something
  12. }
  13. }
  14. }

Output:

  1. A1234567890-MID-12

答案2

得分: 0

Pattern pattern = Pattern.compile("A[0-9]{10}-MID-[0-9]{2}");

for(String line : lines) {
Matcher matcher = pattern.matcher(line);

  1. if (matcher.find()) {
  2. String id = matcher.group();
  3. // ...执行某些操作 < -- 代码从未执行到这里。
  4. }

}

英文:
  1. Pattern pattern = Pattern.compile(&quot;A[0-9]{10}-MID-[0-9]{2}&quot;);
  2. for(String line : lines) {
  3. Matcher matcher = pattern.matcher(line);
  4. if (matcher.find()) {
  5. String id = matcher.group();
  6. // ...Do something &lt;-- The code never reaches here.
  7. }
  8. }

huangapple
  • 本文由 发表于 2020年10月24日 00:53:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64504231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定