Java regular expression with groups

混江龙づ霸主 提交于 2020-01-16 08:51:08

问题


I would like to replace all occurences of strings like:

"{something1}
"{someother2}
"{thing3}

but how to deal with group that contains string, not chars?

-- edit:

e.g. given String:

sometext "{something1}hello

I would like to have

sometext hello

or better, but its only replaceAll parameter

sometext "hello

回答1:


I guess you can use replaceAll:

String b = a.replaceAll("\\{.*?\\}", "sometext ");

This will replace all characters surrounded by curly braces with the replacement string.




回答2:


Just build a regular expression using the | operator inside a group.




回答3:


You could use the or '|' operator to match full strings -

subject.replace(/something1|someother2|thing3/g, ","); 


来源:https://stackoverflow.com/questions/7188791/java-regular-expression-with-groups

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