davido has good advice with Text::Balanced. If you don't use that, at least get out of a totally regex solution and break things apart with Perl code managing the regexs, that way you can see what's going on as you pick things apart.

If you're like me, it's also tough to just leave behind the ideas of "it should have worked", and "why didn't it". Here's my shot at helping with that:

The $innerRe doesn't have a "^" in it to anchor it at the beginning of the string. That means it can skip over any characters it comes accross, including open parens and quotes, until it finds something it wants, like the open parens inside the quote. Then all it has to do is find a matching closing paren and it's done. That's why the part of $innerRe that handles embedded quotes isn't doing it's job, it never comes into play.

That leaves a follow-on question. Shouldn't we just achor the beginning of $innerRe with a "^" followed by "[^()]*" to account for non-paren text? I tried that, but $innerRe is also used recursively later on in the match, and putting "^" at the beginning of $innerRe means you effectively have two occurences of "^", one at the beginning and another later in the regex. So you can't make $InnerRe anchorred at the beginning, and not anchorred at the beginning, all in the same match invocation. You need to do a match, pull the resulting string out, then match that, so that "^" then refers to the string bound to the second match.

And that's what Text::Balanced is for. It will also help handle the case where you've got a double quoted string, where that string contains an escaped double quote. That can be rough to handle in a regex that's trying to do everything else.

Update: Added escapes for square brackets (thanks to Fletch). Added "in it" to second paragraph to be clearer (after seeing ikegami's reading of it).


In reply to Re: A regexp to parse nested brackets containing strings by rodion
in thread A regexp to parse nested brackets containing strings by dfaure

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.