问题
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