Team LiB
Previous Section Next Section

FAQs

Q: 

How do I make a group noncapturing?

To make a group noncapturing, insert ?: inside the opening parenthesis of the group. For example, change (\w) to (?:\w) .

Q: 

Given the expression \w(\d(\w)), what's the capturing index of the rightmost subgroup, (\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) .

Q: 

How is group indexing affected when one of the groups is a noncapturing group?

The noncapturing group isn't counted in any way when group indexes are calculated.

Answers

A: 

To make a group noncapturing, insert ?: inside the opening parenthesis of the group. For example, change (\w) to (?:\w).

A: 

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).

A: 

The noncapturing group isn't counted in any way when group indexes are calculated.


Team LiB
Previous Section Next Section