This is almost a copy of the example from the  "(?PARNO)" discussion in Extended Patterns in perlre. It needs Perl version 5.10+. Note there is an extraneous ) at the end of the  $s test string.

>perl -wMstrict -le "use 5.010; ;; my $s = q{xx AND(random text) xx OR(more (and more)) AND (or (more (too)))) +}; ;; my $intro = qr{ AND | OR }xms; my $nested_parens = qr{ ( \( ( (?: [^()]*+ | (?-2))* ) \) ) }xms; ;; while ($s =~ m{ ($intro) \s* $nested_parens }xmsg) { print qq{\$1 '$1' \$2 '$2' \$3 '$3'}; } " $1 'AND' $2 '(random text)' $3 'random text' $1 'OR' $2 '(more (and more))' $3 'more (and more)' $1 'AND' $2 '(or (more (too)))' $3 'or (more (too))'

Update: Changed
    printf qq{\$1 '$1'  \$2 '$2'  \$3 '$3' \n}, $1, $2, $3;
in while-loop to something that makes more sense.


In reply to Re: simple perl regex question (or is it?) by AnomalousMonk
in thread simple perl regex question (or is it?) by cadphile

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, details, 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, summary, 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.