Java regular expression with groups
-
12-01-2021 - |
Frage
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
Lösung
I guess you can use replaceAll:
String b = a.replaceAll("\\{.*?\\}", "sometext ");
This will replace all characters surrounded by curly braces with the replacement string.
Andere Tipps
Just build a regular expression using the |
operator inside a group.
You could use the or '|' operator to match full strings -
subject.replace(/something1|someother2|thing3/g, ",");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow