Pattern eqPattern = Pattern.compile("(.*?)([a-z0-9\\_\\.]*) eq \"(((\\\\\")|[^\"])*)\"([\\s]*.*)", Pattern.CASE_INSENSITIVE);
这是我的正则表达式。 当我尝试匹配一个长字符串时,出现堆栈溢出。 该模式将匹配类似 column1 eq "abc"和 column ne "abc"; (\\\")|[^\"]): 跳过"in inside""。 我想问一下如何重写这个以防止堆栈溢出。
最佳答案
最好的方法是从正则表达式中删除交替。
可以这样做,它使用展开的循环代替:
"(.*?)([\\w.]*) eq\"([^\"\\\\]*(?:\\\\[\\S\\s] [^\"\\\\]*)*)\"(\\s*.*)"
原始和扩展
( .*? ) # (1)
( [\w.]* ) # (2)
[ ] eq [ ]
"
( # (3 start)
[^"\\]*
(?:
\\ [\S\s]
[^"\\]*
)*
) # (3 end)
"
( \s* .* ) # (4)
关于Java正则表达式匹配堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58701781/