groupCount是用在pattern中有‘()’时使用
如pattern = "(src)='(http)://[^']+'";
找到的m.groupCount 就为2
group的用法如下
输出为:
如果pattern改为 Pattern.compile("#(\\w+)#");
则matcher.groupCount()输出为1
如pattern = "(src)='(http)://[^']+'";
找到的m.groupCount 就为2
m.group() = m.group(0) 是匹配的一整段
m.group(1) = src
m.group(2) = http
group的用法如下
public static void main(String[] args) {
String src = "sss#this#xx#that#df";
Pattern pattern = Pattern.compile("#\\w+#");
Matcher matcher = pattern.matcher(src);
System.out.println("matcher.groupCount():" + matcher.groupCount());
while(matcher.find()){
System.out.println(matcher.group());
}
}
输出为:
matcher.groupCount():0
#this#
#that#
如果pattern改为 Pattern.compile("#(\\w+)#");
则matcher.groupCount()输出为1