
Don't confuse this number with zero based index like in arrays, it is just a group number or more accurately, it is just a group identity. Also it's important to know that, the captured group numbering starts from 1. Group zero denotes the entire recent match.

int Matcher#groupCount(): returns the number of groups captured in the recent match.int Matcher#end(int n): returns the end index of captured group number n.int Matcher#start(int n): returns the start index of captured group number 'n'.String group(int n): returns the substring captured by group number 'n'.Ĭalling oup(0) is equivalent to calling oup().This match is captured as group number 0. int Matcher#end(): The end index of recent match.int Matcher#start(): The start index of the recent match.The returned string is equivalent to s.substring(matcher.start(), matcher.end()), where "s" is the entire input string. String Matcher#group(): returns the recent match.Followings are useful methods of Matcher class to get the information related to capturing groups. There's always a special group number zero which represents the entire match. () will be assigned the number 1 and () will be assigned the number 2. It happens at the time when a match is found. The groups are assigned a number by the regex engine automatically. Other than that groups can also be used for capturing matches from input string for expression.

alteration using logical OR (the pipe '|'). In our basic tutorial, we saw one purpose already, i.e. These groups can serve multiple purposes.

We can combine individual or multiple regular expressions as a single group by using parentheses ().
