Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

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


in reply to Regex's, parentheses, and the mysterious ( ??{ } ) operator

I think you have one too many  our $paren , should be  (??{ $paren })  


Comment on Re: Regex's, parentheses, and the mysterious ( ??{ } ) operator
Select or Download Code
Re^2: Regex's, parentheses, and the mysterious ( ??{ } ) operator
by Clovis_Sangrail (Beadle) on Jul 12, 2013 at 15:35 UTC

    Laurent R: "the $stuff variable is not the same in the two programs:"

    I must've done that after I copied one program to the other, but it does not matter, in both programs that area is inside the first pair of parenthesis, and it gets included in the match.

    Anonymous Monk: "I think you have one too many $parens..."

    I do not know how to say what the problem is any differently. I *want* to use $paren a second time and have it find the *second* separate parenthesized text string within the $stuff variable, and set the $2 regex memory variable equal to it. The regex defined in the variable $paren is set works, it successfully finds the first open '(' and balancing close ')' pair of parenthesis. Because I use it as:

    /($paren)/

    It sets the matching text to the $1 variable. How come I cannot use it again, via something like:

    /($paren)[^()]+($paren)/

    The sample $stuff variable does have a second, separate parenthesized text string in it ( "(for a while)" ) how come the same $paren regex does not find that second parenthsized string and set $2 to it?

      Please try reading that again, because our is our

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

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2014-07-23 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls