Branch Reset
Perl 5.10 introduced a feature where each alternative in a subpattern uses the same numbers for its capturing parentheses. Such a subpattern starts with (?| and is itself a non-capturing subpattern.
This construct is useful when you want to capture part, but not all, of one of a number of alternatives.
Description
Inside a branch reset pattern, capture groups are numbered as usual, but the number is reset at the start of each branch.
The numbers of any capturing groups that follow the subpattern start after the highest number used in any branch.
Description (example)
The numbers underneath show in which group the captured content will be stored.
before -branch-reset- after
(?x) (a) (?|(y)|(t)(v)) ( z )
1 2 2 3 4
The following example is taken from the Perl documentation. The numbers underneath show in which group the captured content will be stored.
before ----------branch-reset-------- after
(?x) (a) (?| x(y)z | (p(q)r) | (t)u(v)) ( z )
1 2 2 3 2 3 4
Nested "branch reset" patterns
As a note, nested branch reset patterns are fully supported:
(?|(1a)(2a)|(1b)(?|(2b1)|(2b2)))
1 2 1 2 2
.NET Numbering
If the DOTNET_NUMBERING flag is set, named capture groups inside of a branch reset pattern will be numbered as if they were unnamed groups. The group remains a named group, and can still be referred by name.
Outside of a branch reset pattern, named groups are numbered as usual.