英文:
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)) {
    //...做一些操作 <-- 代码永远不会执行到这里。
}
英文:
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.

Seeking help here to check if this look like something which can be optimized?
Here's the Java Code -
String pattern1="(.|\n|\r)*(A[0-9]{10}-MID-[0-9]{2})(.|\n|\r)*";
if(file_content.matches(pattern1)) {
//...Do something <-- 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("A[0-9]{10}-MID-[0-9]{2}");
		String fileContent = "A1234567890-MID-12";// 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("A[0-9]{10}-MID-[0-9]{2}");
for(String line : lines) {
    Matcher matcher = pattern.matcher(line);
    if (matcher.find()) {
        String id = matcher.group();
        // ...Do something <-- The code never reaches here. 
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论