java.time.Matcher.find(int start)方法

java.time.Matcher.find(int start)方法重置此匹配器,然后尝试从指定的索引处开始查找与模式匹配的输入序列的下一个子序列。

声明

以下是java.time.Matcher.find(int start)方法的声明。

public boolean find(int start)

参数

  • start - 输入字符串中的起始索引。

返回值

当且仅当从给定索引开始的输入序列的子序列与此匹配器的模式匹配时才为真

异常

IndexOutOfBoundsException - 如果模式中没有具有给定索引的捕获组。

示例

以下示例显示了java.time.Matcher.find(int start)方法的用法。

package com.zaixian;

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);

      if(matcher.find(6)) {
         //Prints the offset after the last character matched.
         System.out.println("First Capturing Group, (a*b) Match String end(): "+matcher.end());    
         System.out.println("Second Capturing Group, (foo) Match String end(): "+matcher.end(1));  
      }
   }
}

执行上面示例代码,得到以下结果:

First Capturing Group, (a*b) Match String end(): 12
Second Capturing Group, (foo) Match String end(): 9

上一篇: Java正则表达式Matcher类 下一篇: Java正则表达式PatternSyntaxException类