How are nested capturing groups numbered in regular expressions?

From perlrequick

If the groupings in a regex are
nested, $1 gets the group with the
leftmost opening parenthesis, $2 the
next opening parenthesis, etc.

Caveat: Excluding non-capture group opening parenthesis (?=)

Update

I don’t use PCRE much, as I generally use the real thing ;), but PCRE’s docs show the same as Perl’s:

SUBPATTERNS

2. It sets up the subpattern as a capturing subpattern. This means that, when the whole pattern matches, that portion of the subject string that matched the subpattern is passed back to the caller via the ovector argument of pcre_exec(). Opening parentheses are counted from left to right (starting from 1) to obtain number for the capturing subpatterns.

For example, if the string “the red king” is matched against the pattern

the ((red|white) (king|queen))

the captured substrings are “red king”, “red”, and “king”, and are numbered 1, 2, and 3, respectively.

If PCRE is drifting away from Perl regex compatibility, perhaps the acronym should be redefined–“Perl Cognate Regular Expressions”, “Perl Comparable Regular Expressions” or something. Or just divest the letters of meaning.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)