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

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

StackOverflowError when matching longer string using Regex - Optimization

问题

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

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

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

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

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

这是 Java 代码 -

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

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

英文:

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

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 -

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

}

答案1

得分: 0

你不必要地使用了 `(.|\n|\r)*`。以下是更清晰更高效的方法

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
        public static void main(String[] args) {
            Pattern pattern = Pattern.compile("A[0-9]{10}-MID-[0-9]{2}");
            String fileContent = "A1234567890-MID-12"; // 将其替换为实际内容
            Matcher matcher = pattern.matcher(fileContent);
            while (matcher.find()) {
                String id = matcher.group(); // 如果你想获取ID
                System.out.println(id);
                // ...做点什么
            }
        }
    }

**输出:**

    A1234567890-MID-12
英文:

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

Output:

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);

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

}

英文:
Pattern pattern = Pattern.compile(&quot;A[0-9]{10}-MID-[0-9]{2}&quot;);

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

    if (matcher.find()) {
        String id = matcher.group();
        // ...Do something &lt;-- The code never reaches here. 
    }
}

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:

确定