in reply to Re^5: Regex's, parentheses, and the mysterious ( ??{ } ) operator
in thread Regex's, parentheses, and the mysterious ( ??{ } ) operator
... $2 is still blank.
You have to properly account for all capturing groups in the overall regex, properly counting capture groups in any interpolated regex. If you have a capturing group within the interpolated recursive regex (not, IMHO, necessary), then you want to access the 3rd capture group variable. However, a capture group in the recursive regex screws up the more general
@p = $s =~ m{ $r4 }xmsg;
extraction regex. See examples below.
>perl -wMstrict -le "my $s = 'x(y) (a(b)) ()() q (a(b)c()(d(e(f)g))h) q'; ;; our $r3 = qr{ \( (?: [^()]+ | (??{ our $r3 }) )* \) }xms; ;; my @p = $s =~ m{ $r3 }xmsg; print qq{'$_'} for @p; print '--------'; ;; $s =~ m{ ($r3) [^()]* ($r3) }xms; print qq{1 '$1' 2 '$2'}; print '--------'; ;; our $r4 = qr{ \( ( [^()]* | (??{ our $r4 }) )* \) }xms; ;; @p = $s =~ m{ $r4 }xmsg; print qq{'$_'} for @p; print '--------'; ;; $s =~ m{ ($r4) [^()]* ($r4) }xms; print qq{1 '$1' 3 '$3'}; print '--------'; " '(y)' '(a(b))' '()' '()' '(a(b)c()(d(e(f)g))h)' -------- 1 '(y)' 2 '(a(b))' -------- '' '' '' '' '' -------- 1 '(y)' 3 '(a(b))' --------
Have you tried the very neat "(?PARNO)" operator available with Perl 5.10+ and discussed in the example referred to here?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Regex's, parentheses, and the mysterious ( ??{ } ) operator
by Clovis_Sangrail (Beadle) on Jul 15, 2013 at 17:08 UTC | |
by AnomalousMonk (Archbishop) on Jul 15, 2013 at 19:24 UTC |