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

Comment on

by gods
on Feb 11, 2000 at 00:06 UTC ( #3333=superdoc: print w/replies, xml ) Need Help??

The excellent Simon Cozens tutorial at

http://www.perl.com/pub/2003/06/06/regexps.html

shows how to use regex's to isolate parenthesised text within a string with handling of nested/multiple parentheses. Cozens constructs a recursive Regex, and uses the ( ??{ $regex_variable } ) construct to defer interpolation of the variable until execution. It works for me!:

$ cat p2.pl #!/opt/perl5.16/bin/perl use strict; use warnings; our $paren = qr/ # Need declared variable with use stri +ct. \( ( [^()]+ # Not parens | (??{ our $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) + ( further ))) but still (here) ) and now (for a while) we are out ag +ain."; $stuff =~ /($paren)/; print "----------\n"; print "$stuff\n"; print "----------\n"; print $1 . "\n"; print "----------\n";
$ ./p2.pl ---------- On the outside now then (we go( in( and in (&stop)(awhile) ( further ) +)) but still (here) ) and now (for a while) we are out again. ---------- (we go( in( and in (&stop)(awhile) ( further ))) but still (here) ) ---------- $

But if I try to use the regex again it does not work as I think it ought. In the above code when I replace the matching regex with /($paren)^()*($paren)/ I expect $2 to be "(for a while)", but that does not work:

$ cat p3.pl #!/opt/perl5.16/bin/perl use strict; use warnings; our $paren = qr/ # Need declared variable with use stri +ct. \( ( [^()]+ # Not parens | (??{ our $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 +) ( further ))) but still (here) ) and now (for a while) we are out a +gain."; $stuff =~ /($paren)[^()]*($paren)/; print "----------\n"; print "$stuff\n"; print "----------\n"; print $1 . "\n"; print "----------\n"; print $2 . "\n"; print "----------\n"; $ ./p3.pl ---------- On the outside now then (we go( in( and in (&stop)(awhile) ( further ) +)) but still (here) ) and now (for a while) we are out again. ---------- (we go( in( and in (&stop)(awhile) ( further ))) but still (here) ) ---------- ---------- $

$2 is empty. Does anyone know what is going on here? If I take out the ^()+ portion of the regex, then the program hangs when I run it.


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (12)
    As of 2016-10-01 19:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?






      Results (6 votes). Check out past polls.