So you had some working code, you made a change, and problems ensued?

Perhaps if you showed us the change you made we could see what you did wrong. At a random shout in the dark you inserted stuff inside the recursive part of the pattern. PCRE now supports the reusable sub-expression syntax.

The problem of matched parentheses is the most commonly quoted example of using this construct.

my $bal = qr/ (?<bal> # Name the rule (optional) \{ # Open brace (?> # Possessive subgroup (?> [^{}]+ ) # Grab all the non braces | # or (?&bal) # Recurse )* # Zero or more times \} # Close brace ) # End named rule /x; if ('{x{x}y{x}x}' =~ /^$bal$/ ){ print "It's balanced\n"; } $_= 'XXXX function xxx() {x{x}y{x}x} XXXX'; while ( /\bfunction\s+(\w+)\(\)\s*($bal)/g ){ print "function: $1\nbody: $2\n"; }

This, of course, is Perl code - adapting it to PHP is left as a exercise for the reader.

I should add, this only checks for nested {} it doesn't know to ignore {} within quotes - that too is possible by hybridising the above with the tricks for parsing CSV.


In reply to Re^3: Recursive Regular Expression Help by nobull
in thread Recursive Regular Expression Help by me

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.