A capturing group is a method that you can use to group multiple characters inside a set of parenthesis. Count the opening parentheses from left to right to enumerate the capturing group.
In the expression ((A)(B(C))), there are four groups: ((A)(B(C))), (A), (B(C)), and (C). Group zero stands represents the whole expression.
The captured characters in a group are the most recent sub-sequence that the group matched. All captured input is discarded at the beginning of each match. If a group is matched again, the previously captured value is retained if the second evaluation fails. For example, the matching the string "aba" in the expression
(a(b)?)+
changes group two set to
(B)
.
Groups that start with the expression
(?
do not capture text and do not count toward the group total.