java.util.regex.Matcher.reset() 方法

描述

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


声明

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

public Matcher reset()

返回值

此匹配器。


示例

下面的例子展示了 java.util.regex.Matcher.reset() 方法的用法。

package com.tutorialspoint;

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

❮ java 正则表达式 - Matcher 匹配器