Matcher reset()

优质
小牛编辑
127浏览
2023-12-01

描述 (Description)

java.util.regex.Matcher.reset()方法重置此匹配器。

声明 (Declaration)

以下是java.util.regex.Matcher.reset()方法的声明。

public Matcher reset()

返回值 (Return Value)

这个匹配器。

例子 (Example)

以下示例显示了java.util.regex.Matcher.reset()方法的用法。

package cn.xnip;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherDemo {
   private static String REGEX = "(a*b)(foo)";
   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);   
      matcher.region(10, 15);
      System.out.println("Before reset, regionStart(): " + matcher.regionStart());
      System.out.println("Before reset, regionEnd(): " + matcher.regionEnd());
      matcher.reset();
      System.out.println("After reset, regionStart(): " + matcher.regionStart());
      System.out.println("After reset, regionEnd(): " + matcher.regionEnd());
   }
}

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

Before reset, regionStart(): 10
Before reset, regionEnd(): 15
After reset, regionStart(): 0
After reset, regionEnd(): 18