简体   繁体   English

带组的Java正则表达式

[英]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 或更好,但是它只有replaceAll参数

sometext "hello

I guess you can use replaceAll : 我猜你可以使用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, ","); 

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

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