目录

int groupCount()

描述 (Description)

java.time.Matcher.groupCount()方法返回此匹配器模式中的捕获组数。

声明 (Declaration)

以下是java.time.Matcher.groupCount()方法的声明。

public int groupCount()

返回值 (Return Value)

此匹配器模式中的捕获组数。

例子 (Example)

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

package com.iowiki;
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";
   public static void main(String[] args) {
      Pattern pattern = Pattern.compile(REGEX);
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT);
      if(matcher.find()) {
         //Prints the number of capturing groups in this matcher's pattern.
         System.out.println("Group Count: "+matcher.groupCount());    
      }      
   }
}

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

Group Count: 2
↑回到顶部↑
WIKI教程 @2018