java.time.Matcher.groupCount()
方法返回此匹配器模式中的捕獲組數。
聲明
以下是java.time.Matcher.groupCount()
方法的聲明。
public int groupCount()
返回值
此匹配器模式中的捕獲組數。
示例
以下示例顯示了java.time.Matcher.groupCount()
方法的用法。
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";
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