matcher.appendReplacement …

matcher.appendReplacement

这个方法好像很好用啊,明天试一试,应该就是做我今天要做的事情的方法。hoho

Pattern p = Pattern.compile("cat");
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());

Leave a Reply