In addition to @revo answer :
There is not only a conditional construction with an explicit statement of zero width, as well as its expression. In fact, almost all conditional constructions where condition expressions are copied regular expressions (grouping, conditional, other special) used without additional brackets.
In such cases, there are four types of (incorrect) behavior:
The capture of a group array becomes distorted (as the OP indicates), namely: the capture group is lost immediately after the conditional construction; the remaining groups are shifted to the left, leaving the last capture group undefined.
In the following examples, the expected capture distribution is
$1="a", $2="b", $3="c"
whereas the actual result
$1="a", $2="c", $3="" (the latter is empty string)
Refers to:
Throws an ArgumentException during regular expression parsing. It really makes sense, as it clearly warns us of some potential regular expression error, rather than playing fun tricks with captures, as in the previous case.
Refers to:
(a)(?(?<n>.) )(b) (c) , (a)(?(?'n'.) )(b) (c) - named groups - exception message: "Alternation conditions do not capture and cannot be named"(a)(?(?'-n' .) )(b) (c) , (?<a>a)(?(?<an>.) )(b) (c) - balancing groups - message about exception: "Alternation conditions do not capture and cannot be named"(a)(?(?# comment) )(b) (c) - embedded comment - exception message: "Alternation conditions cannot be comments"
OutOfMemoryException during pattern matching. This is obviously a mistake, I suppose.
Refers to:
(a)(?(?i) )(b) (c) - built-in parameters (not to be confused with group parameters)
[Surprisingly] works as expected, but this is too artificial an example:
All of these regular expressions can be corrected by including the condition expression in an explicit bracket (i.e., optional if the expression itself already contains brackets). Here are the fixed versions (in order of appearance):
(a)(?((?=.)) )(b) (c) (a)(?((?!z)) )(b) (c) (a)(?((?<=.)) )(b) (c) (a)(?((?<! )) )(b) (c) (a)(?((?: )) )(b) (c) (a)(?((?i:.)) )(b) (c) (a)(?((?>.)) )(b) (c) (a)(?((?(1).)) )(b) (c) ((?<n>a))(?((?(n).)) )(b)(c) (a)(?((?(?:.).)) )(b) (c) (a)(?((?<n>.)) )(b) (c) (a)(?((?'n'.)) )(b) (c) (a)(?((?'-n' .)) )(b) (c) (?<a>a)(?((?<an>.)) )(b) (c) (a)(?((?
Sample code to test all of these expressions: https://ideone.com/KHbqMI
source share