应该还有更好的办法,以下只为实现功能 /* * 完整替换字符串,如:2dashu_16/2dashu_1)*100,只替换:2dashu_1,不会替换:2dashu_16 * str:字符串 * old:要替换单词 * newWord:替换后单词 */ public static String completeReplace(String str,String old,String newWord){ Pattern p = Pattern.compile(old + "([^_//d//w])"); Matcher m2 = p.matcher(str); StringBuffer sb = new StringBuffer(); while (m2.find()) { m2.appendReplacement(sb, newWord+m2.group(1)); } m2.appendTail(sb); return sb.toString(); }