How do I make a group noncapturing? | ||
Given the expression \w(\d(\w)), what's the capturing index of the rightmost subgroup, (\w)? | ||
How is group indexing affected when one of the groups is a noncapturing group? |
Answers
To make a group noncapturing, insert ?: inside the opening parenthesis of the group. For example, change (\w) to (?:\w). |
|
Groups are counted from left to right, starting with the opening parenthesis, and group(0) always refers to the whole regex pattern. Thus, the capturing index of subgroup (\w) is 2. Accordingly, the capturing index of (\d(\w)) is group(2). |
|
The noncapturing group isn't counted in any way when group indexes are calculated. |