J
Jon Shemitz
I have a regex that matches nested parens (it's a slightly rewritten
version of the one in Friedl's "Mastering Regular Expressions, 2e"):
#[IgnorePatternWhitespace]
\( # a literal (
(?: # non-capture group
\( (?<Stack>) # on nested (, push empty capture
| \) (?<-Stack>) # on nested ), pop empty capture
| [^()] # anything except ( or )
)* # any number of chars between parens
(?(Stack) # if stack not empty:
^ # then, match beginning of string (ie, fail)
| \) ) # else, match literal )
Now, this works just fine, even on empty parens "()". What I don't
understand is why I have to use /[^()]/ instead of "." in the clause
that matches anything except parens.
As I understand alternation, the left alternative is matched first. If
it matches, the right alternative is skipped. (This certainly seems to
explain the capture behavior when I match /that|th([a-z])t/ against
"that thought": on the first Match, the second Group fails to match;
while on the second Match, the second Group matches "ough".)
Why, then, doesn't /./ work instead of /[^()]/? Since a left or right
paren should have already been matched ....
version of the one in Friedl's "Mastering Regular Expressions, 2e"):
#[IgnorePatternWhitespace]
\( # a literal (
(?: # non-capture group
\( (?<Stack>) # on nested (, push empty capture
| \) (?<-Stack>) # on nested ), pop empty capture
| [^()] # anything except ( or )
)* # any number of chars between parens
(?(Stack) # if stack not empty:
^ # then, match beginning of string (ie, fail)
| \) ) # else, match literal )
Now, this works just fine, even on empty parens "()". What I don't
understand is why I have to use /[^()]/ instead of "." in the clause
that matches anything except parens.
As I understand alternation, the left alternative is matched first. If
it matches, the right alternative is skipped. (This certainly seems to
explain the capture behavior when I match /that|th([a-z])t/ against
"that thought": on the first Match, the second Group fails to match;
while on the second Match, the second Group matches "ough".)
Why, then, doesn't /./ work instead of /[^()]/? Since a left or right
paren should have already been matched ....