[英]Regular Expression issue pattern matching
I am trying to write a regular expression for below string pattern 我正在尝试为以下字符串模式编写正则表达式
String str = "<name>{f:<one>,l:<two>,t:<three>} )";
this pattern ends with "?" 此模式以“?”结尾 so below code works 所以下面的代码有效
String result = str.replaceAll(pattern, "abc");
System.out.println(result);
and the output is 输出是
<name> ( abc abc)
But if the input string changes 但是如果输入字符串改变
where next pattern doesn't end with ?, Java hung..... 下一个模式不以?结尾的地方,Java挂起了.....
String result = str.replaceAll(pattern, "abc");
System.out.println(result);
Equivalent Scala code is 等效的Scala代码是
pattern.replaceAllIn(str,"abc")
Where am I wrong, thanks in advance 我在哪里错了,先谢谢
From you example it is hard to tell what you try to achieve here are some remarks how the regex could be improved: 从您的示例中很难看出您要实现的目标,这里有一些说明可以改善正则表达式:
There are several strange things in your regex 正则表达式中有几件奇怪的事情
(.)*
could create an infinite number of relatively expensive catching groups if you do not want to use the result later use (?:)
or at least change to (.*)
如果您不想在以后使用(?:)
或至少更改为(.*)
来使用结果,则(.)*
可以创建无数个相对昂贵的捕获组(.*)
<\\\\S+?>
is better expressed as <[^>]+>
<\\\\S+?>
最好表示为<[^>]+>
As with most of your matchers you use the non-greedy approach you may run in a bad backtracking issue which could be avoided by changing the regex. 与大多数匹配器一样,您使用的是非贪婪方法,因此可能会遇到回溯问题,可以通过更改正则表达式来避免。
it should work, if you change your regex to ie * at end 如果将正则表达式更改为ie *,它将正常工作
String pattern = "\\{(((.)*?(<\\S+?>)?)+?)\\}\\?*";
or simply 或简单地
String pattern = "\\{.*?(\\})(\\?)*";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.