Not only possessive quantifiers, but also the  (?1) family of extended patterns were introduced with 5.10. However, the 'recursive parsing' trick can still be done with 5.8, so if 5.10/5.12 cannot be installed, check back here for more info. (But see Update below.)

(See Text::Balanced for all of the following functionality – and there's more!)

Following code requires 5.10+.

I find it useful to decompose regexes. (Closing sequence arbitrarily redefined to ']]' in example; could be any multi-character sequence.)

>perl -wMstrict -le "my $open = '{{'; my $close = ']]'; ;; my $opener = qr{ \Q$open\E }xms; my $closer = qr{ \Q$close\E }xms; my $body = qr{ [^\Q$open$close\E] }xms; ;; my $regex = qr{ ( $opener (?: $body++ | (?1) )* $closer ) }xms; ;; my $s = 'xxx {{ foo {{ bar ]] baz ]] yyy {{ fee ]] zzz'; ;; print qq{'$1'} while $s =~ m{ $regex }xmsg; " '{{ foo {{ bar ]] baz ]]' '{{ fee ]]'

This approach breaks down when we alter the string being searched to
    my $s =
      'xxx {{ foo {{ bar ]] baz [OK] ]] [NO] yyy {{ fee ]] zzz';
producing the output
    '{{ bar ]]'
    '{{ fee ]]'
because of the presence of the substring '[OK]' having the character ']' from the closing sequence.

This problem can be fixed by changing the definition of  $body to
    my $body = qr{ (?! $opener) (?! $closer) . }xms;
which restores the output to the expected
    '{{ foo {{ bar ]] baz [OK] ]]'
    '{{ fee ]]'
again.

Update: Oh, what the heck... Here's the 5.8.9 version:

>perl -wMstrict -le "print qq{perl version $]}; ;; my $opener = qr{ \{\{ }xms; my $closer = qr{ \]\] }xms; my $body = qr{ (?! $opener) (?! $closer) . }xms; ;; use re 'eval'; our $regex = qr{ $opener (?: (?> $body+) | (??{ $regex }) )* $closer }xms; ;; my $s = 'xxx {{ foo {{ bar ]] baz [OK] ]] [NO] yyy {{ fee ]] zzz'; ;; print qq{'$1'} while $s =~ m{ ($regex) }xmsg; " perl version 5.008009 '{{ foo {{ bar ]] baz [OK] ]]' '{{ fee ]]'

In reply to Re: ERROR ... nested quantifiers in regex by AnomalousMonk
in thread ERROR ... nested quantifiers in regex by ŞuRvīvőr

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.