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



描述

java.util.regex.Matcher.reset(CharSequence input) 方法用新的輸入序列重置此匹配器。

宣告

以下是 java.util.regex.Matcher.reset(CharSequence input) 方法的宣告。

public Matcher reset(CharSequence input)

引數

  • input − 新的輸入字元序列。

返回值

此匹配器。

示例

以下示例演示了 java.util.regex.Matcher.reset(CharSequence input) 方法的使用方式。

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 INPUT1 = "fooabfoob";
   private static String REPLACE = "-";
   
   public static void main(String[] args) {
      Pattern pattern = Pattern.compile(REGEX);
      
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT);   
      
      while(matcher.find()) {
         //Prints the offset after the last character matched.
         System.out.println("First Capturing Group, (a*b) Match String end(): "+matcher.end());   
      }     
      
      matcher.reset(INPUT1);
      System.out.println("RESET");
      
      while(matcher.find()) {
         //Prints the offset after the last character matched.
         System.out.println("First Capturing Group, (a*b) Match String end(): "+matcher.end());   
      }     
   }
}

讓我們編譯並執行上述程式,此操作將生成以下結果 −

First Capturing Group, (a*b) Match String end(): 6
First Capturing Group, (a*b) Match String end(): 12
First Capturing Group, (a*b) Match String end(): 17
RESET
First Capturing Group, (a*b) Match String end(): 8
javaregex_matcher.htm
廣告
© . All rights reserved.