Capturing groups are numbered in increasing numbers, starting with number one. Java regular expressions support up to 99 capturing groups. Group zero always stands for the entire matched text.
For nested capturing groups, group numbers are incremented with the appearance of the opening parenthesis from left to right.
To understand this better, let's consider the following regular expression with nested multiple capturing groups:
(((a|b)-(c|d))/(\d+))
It will match the input string as follows:
a-c/15 a-d/99 b-c/567 b-d/1000
For the input string, a-c/15, we will get the following captured groups:
Group Num |
Captured Text |
Group 0 |
a-c/15 |
Group 1 |
a-c/15 |
Group 2 |
a-c |
Group 3 |
a |
Group 4 |
c |