您的答案在Matcher#appendReplacement文档中。只需将您的函数调用放入while循环中即可。
[appendReplacement方法]旨在与appendTail和find方法一起在循环中使用。例如,以下代码将院子里的两只狗写到标准输出流中:
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());


