简体   繁体   中英

Java regular expression with groups

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:

eg given String:

sometext "{something1}hello

I would like to have

sometext hello

or better, but its only replaceAll parameter

sometext "hello

I guess you can use replaceAll :

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

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

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, ","); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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