// where does m.groupCount come from m = Pattern.compile("(group1)(group2)(group3)").matcher(html); System.out.println(m.groupCount()); //3
Add explanation, see the source code comments
/** * Returns the number of capturing groups in this matcher's pattern. * *
Group zero denotes the entire pattern by convention. It is not * included in this count. * *
Any non-negative integer smaller than or equal to the value * returned by this method is guaranteed to be a valid group index for * this matcher.
* * @return The number of capturing groups in this matcher's pattern */ public int groupCount() { return parentPattern.capturingGroupCount - 1; }
It is clear here that the result ofgroupCount返回的是正则表达式的捕获分组的数量(捕获分组和非捕获分组是另外的知识点),groupCountdoes not indicate the result of the match.
To perform regular expression matching, you need to perform thefindaction, see the source code
public boolean find() { int nextSearchIndex = last; if (nextSearchIndex == first) nextSearchIndex++; // If next search starts before region, start it at region if (nextSearchIndex < from) nextSearchIndex = from; // If next search starts beyond region then it fails if (nextSearchIndex > to) { for (int i = 0; i < groups.length; i++) groups[i] = -1; return false; } return search(nextSearchIndex); }
This will assign a value to the member variablegroupsinsideMatcher,groups[i] = -1;Matcher内部的成员变量groups赋值,groups[i] = -1; 这样的之后在我们执行m.group(1)After we execute thism.group(1)Only when we get the content matched by the capture group.
I imitated the meaning of the question and wrote the test code. The results are as follows
Also
Add explanation,
see the source code comments
It is clear here that the result of
groupCount
返回的是正则表达式的捕获分组的数量(捕获分组和非捕获分组是另外的知识点),groupCount
does not indicate the result of the match.To perform regular expression matching, you need to perform the
find
action, see the source codeThis will assign a value to the member variable
groups
insideMatcher
,groups[i] = -1;
Matcher
内部的成员变量groups
赋值,groups[i] = -1;
这样的之后在我们执行
m.group(1)
After we execute thism.group(1)
Only when we get the content matched by the capture group.