问题
I have this regex which is a bit longer than usual. I try to capture some values in a text document.
\\n*.*(k\\s=\\s\\d)(.|\\n)*?estimate\\s.*\\n*\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s+
It works perfectly fine on regexr.com link
but in Java only this part works
\\n*.*(k\\s=\\s\\d)(.|\\n)*?estimat
as soon as I add the missing 'e' it stops working.
For now I am ignoring that some groups are filled wrongly.
What goes wrong?
回答1:
The (.|\\n)*? makes the regex engine perform too many redundant backtracking steps. You need to replace all such parts in your pattern with (?s:.*?), a modifier group that matches any 0+ chars including line break chars. Since there is no alternation, there is no redundant backtracking here.
Note that in JavaScript (as you are testing the pattern at regexr.com that only supports JavaScript regex flavor), the (.|\n)*? should be replaced with [^]*? or [\s\S]*? as its regex engine does not support inline modifiers at all.
来源:https://stackoverflow.com/questions/46054741/java-regex-pattern-too-long