Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Regex's, parentheses, and the mysterious ( ??{ } ) operator

by Anonymous Monk
on Jul 12, 2013 at 15:49 UTC ( #1044008=note: print w/ replies, xml ) Need Help??

Comment on Re^3: Regex's, parentheses, and the mysterious ( ??{ } ) operator
Re^4: Regex's, parentheses, and the mysterious ( ??{ } ) operator
by Clovis_Sangrail (Beadle) on Jul 12, 2013 at 16:05 UTC

    "... our = our"

    Yes, I am familiar with the reflexive property of equality. (For all X, X == X.) Can you be a little less enigmatic? Are you saying I cannot use the "our" scope? When I used "my" I created an entirely separate $paren and broke the recursion, and because I have "use strict" I cannot leave the variable undeclared. Perhaps I just cannot use "use strict"?

      ... our is our

      I think Anonymonk is concerned about the second appearance of  our $re within the  (??{ our $re }) regex subexpression. Although somewhat unfamiliar, I think this is kosher because our acts as a declaration of a package variable and  $re needs to be declared or pre-declared somehow to be referenced within the regex under full strictures. The following variations work identically with strictures (note no capturing groups within $re):

      >perl -wMstrict -le "my $s = 'x(y) (a(b)) ()() q (a(b)c()(d(e(f)g))h) q'; ;; our $r3; $r3 = qr{ \( (?: [^()]+ | (??{ $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 '--------'; " '(y)' '(a(b))' '()' '()' '(a(b)c()(d(e(f)g))h)' -------- 1 '(y)' 2 '(a(b))' --------

      And the OPed:

      our $r3 = qr{ ... (??{ our $r3 }) ... }xms;
Re^4: Regex's, parentheses, and the mysterious ( ??{ } ) operator
by Clovis_Sangrail (Beadle) on Jul 12, 2013 at 16:21 UTC

    Ahh! I have deciphered the meaning of your koan. I get a compilation error when I take the "our" out of the middle of the recursive regex:

    $ vi p6.pl "p6.pl" 36 lines, 925 characters #!/opt/perl5.16/bin/perl use strict; use warnings; our $paren = qr/ # Need declared variable with use stri +ct. \( ( [^()]+ # Not parens | (??{ $paren }) # Another balanced group (not interpol +ated yet) )* \) /x; # 'x' means ignore whitespace, comment +s. my $stuff = "On the outside now then (we go( in( and in (&stop)(awhile +) ( furthe r ))) but still (here) ) and now (for a while) we are out again."; $stuff =~ /($paren)[^()]*($paren)/; print "----------\n"; print "$stuff\n"; print "----------\n"; "p6.pl" 27 lines, 650 characters $ ./p6.pl Variable "$paren" is not imported at (re_eval 1) line 2. Global symbol "$paren" requires explicit package name at (re_eval 1) l +ine 2. Compilation failed in regexp at ./p6.pl line 14. $

      If I delete the "use strict;" the program compiles/runs, but still does not match the 2nd regex. I also tried making a duplicate of $paren called $par2, and used it instead of $paren to set $2, and that doesn't work either, $2 is still blank.

        ... $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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044008]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-12-28 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls