[英]Remove “${anything}” from string in java
I want to remove ${anything} or ${somethingelse} from a string, but i dont find the regex. 我想从字符串中删除$ {anything}或$ {somethingelse},但我找不到正则表达式。
My actual code 我的实际代码
String url = http://test.com/index.jsp?profil=all&value=${value}
String regex = "\\$\\{*\\}";
url = url .replaceAll(regex, ""); // expect http://test.com/index.jsp?profil=all&value=
//but it is http://test.com/index.jsp?profil=all&value=${value}
i'm sure the solution is stupid, but no way to find. 我确定解决方案是愚蠢的,但无法找到。
Try this one: 试试这个:
"\\$\\{.*?\\}"
The .*?
.*?
matches the shortest possible string that is followed by }
. 匹配后面跟着的最短字符串
}
。
you're removing any number of {
's, because you have {*
instead of .*
你要删除任意数量的
{
,因为你有{*
而不是.*
should be \\\\$\\\\{.*\\\\}
应该是
\\\\$\\\\{.*\\\\}
that will indeed allow anything between the braces, do you want that to be alpha only or something? 这确实会允许大括号之间的任何东西,你想要它只是alpha或什么?
that would be \\\\$\\\\{[a-zA-Z]*\\\\}
这将是
\\\\$\\\\{[a-zA-Z]*\\\\}
另一个解决方案是\\\\$\\\\{[^\\\\}]*\\\\}
(任何不同于}的字符)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.