JDK自带的有趣的例子
Pattern p = Pattern.compile("cat\\b"); Matcher m = p.matcher("one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb,"dog"); } m.appendTail(sb); System.out.println(sb.toString());
自己加个字边界匹配,sb出来以后更好理解
原文链接:/regex/358005.html