目录

Matcher appendReplacement(StringBuffer sb, String replacement)

描述 (Description)

java.time.Matcher.appendReplacement(StringBuffer sb, String replacement)方法实现了非终端追加和替换步骤。

声明 (Declaration)

以下是java.time.Matcher.appendReplacement(StringBuffer sb, String replacement)方法的声明。

public Matcher appendReplacement(StringBuffer sb, String replacement)

参数 (Parameters)

  • sb - 目标字符串缓冲区。

  • replacement - 替换字符串。

返回值 (Return Value)

这个匹配器。

异常 (Exceptions)

  • IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败。

  • IllegalArgumentException - 如果替换字符串引用模式中不存在的命名捕获组。

  • IndexOutOfBoundsException - 如果替换字符串引用模式中不存在的捕获组。

例子 (Example)

以下示例显示了java.time.Matcher.appendReplacement(StringBuffer sb,String replacement)方法的用法。

package com.iowiki;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherDemo {
   private static String REGEX = "a*b";
   private static String INPUT = "aabfooaabfooabfoob";
   private static String REPLACE = "-";
   public static void main(String[] args) {
      Pattern pattern = Pattern.compile(REGEX);
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT);
      StringBuffer buffer = new StringBuffer();
      while(matcher.find()) {
         matcher.appendReplacement(buffer, REPLACE);
      }
      matcher.appendTail(buffer);
      System.out.println(buffer.toString());
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

-foo-foo-foo-
↑回到顶部↑
WIKI教程 @2018