使用String的replaceAll()方法:
String regex = "abc";
String newSubstr = "11";
String str = "aabcsdofhds.dfh.abccc.of";
Pattern pattern = Pattern.complie(regex);
Match matcher = pattern.matcher(str);
while(matcher.find()){
String temp = str.substring(matcher.start(),matcher.stop());
str = str.replaceAll(temp,newSubstr);
}
System.out.println(str);
输出结果是:a11sdofhds.dfh.11cc.of
即是把字符串中所有的abc替换成11