例如,我有一个这样的字符串,我必须找到并替换多个子字符串,所有子字符串都#包含6个符号,结尾'并且不应包含)......您认为实现这一目标的最佳方法是什么?
谢谢!
编辑:还有一件事我忘了,要做替换,我需要那个子串,即它被替换为从被替换的子串生成的字符串.
yourNewText=yourOldText.replaceAll("#[^)]{6}'", "");
Run Code Online (Sandbox Code Playgroud)
或以编程方式:
Matcher matcher = Pattern.compile("#[^)]{6}'").matcher(yourOldText);
StringBuffer sb = new StringBuffer();
while(matcher.find()){
matcher.appendReplacement(sb,
// implement your custom logic here, matcher.group() is the found String
someReplacement(matcher.group());
}
matcher.appendTail(sb);
String yourNewString = sb. toString();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
531 次 |
| 最近记录: |