简体   繁体   English

Java正则表达式截断字符串

[英]Java regular expression truncates string

I have the following Java string replaceAll function with a regular expression that replaces with zero variables with format ${var} : 我有以下带有正则表达式的Java字符串replaceAll函数,该正则表达式将零变量替换为${var}格式:

String s = "( 200828.22 +400000.00 ) /  ( 2.00 + ${16!*!8!1} ) + 200828.22 + ${16!*!8!0}";
s = s.replaceAll("\\$\\{.*\\}", "0");

The problem is that the resulting string s is: 问题在于结果字符串s为:

"( 200828.22 +400000.00 ) /  ( 2.00 + 0"

What's wrong with this code? 此代码有什么问题?

Change your regex to 将您的正则表达式更改为

\\$\\{.*?\\}
        ↑

* is greedy , the engine repeats it as many times as it can, so it matches { , then match everything until last token. *贪婪的 ,引擎会重复它多次,因此它匹配{ ,然后匹配所有内容,直到最后一个标记。 It then begins to backtrack until it matches the last character before } . 然后,它开始回溯,直到与}之前的最后一个字符匹配。

For example, if you have the regex 例如,如果您有正则表达式

\\{.*\\} 

and the string 和字符串

"{this is} a {test} string" 

it'll match as follows: 匹配如下:

  • { matches the first { {与第一个{
  • .* matches everything until g token .*匹配所有内容,直到g令牌
  • the regex fails to match last } in the string 正则表达式无法匹配字符串中的最后一个}
  • it backtracks until it reaches t , then it can match the next } resulting with matching "{this is} a {test}" 它回溯直到达到t ,然后可以匹配下一个}从而匹配“ {this is} a {test}”

In order to make it ungreedy, you should add an ? 为了使其变得不贪心,您应该添加一个? . By doing that, it'll become lazy and stops until first } is encountered. 这样,它将变得很懒,直到遇到第一个}为止。

As mentioned in the comments, an alternative would be [^}]* . 如评论中所述,替代方法是[^}]* It matches anything that's not } (since it's placed in a character class ). 它与任何 }匹配(因为它已放置在字符类中 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM