in reply to A regexp to parse nested brackets containing strings
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).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: A regexp to parse nested brackets containing strings
by ikegami (Patriarch) on Sep 23, 2006 at 21:35 UTC | |
by rodion (Chaplain) on Sep 24, 2006 at 08:27 UTC | |
by ikegami (Patriarch) on Sep 24, 2006 at 12:25 UTC |