Java regex pattern too long? [closed]

对着背影说爱祢 提交于 2021-01-28 05:31:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!