简体   繁体   English

如何在Java中替换Curly大括号?

[英]How to Replace Curly braces in java?

I am getting error when try to replace regular expression in java. 我试图在java中替换正则表达式时遇到错误。

for example: 例如:

String h = "{hiren:}";

h=h.replaceAll(":}", ":\"\"}");

Please give me solution. 请给我解决方案。 Thanks 谢谢

You need to double-escape some special characters in Pattern s. 你需要双重转义Pattern的一些特殊字符。

String#replaceAll takes regular expressions, hence: String#replaceAll采用正则表达式,因此:

String h = "{hiren:}"; h=h.replaceAll(":\\}", ":\"\"}");

Otherwise, you can use String#replace with no regular expression nor escaping needed. 否则,您可以使用String#replace而不使用正则表达式,也不需要转义。

String h = "{hiren:}"; h=h.replace(":}", ":\"\"}");

It's a commonly mistaken assumption to believe String#replace will not replace all occurrences. 认为String#replace不会取代所有出现都是一个常见的错误假设。

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

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