Forum Moderators: bakedjake
I'm having some problems building a regular expression I need. I'm trying to match the outermost of a set of braces, and return the contents as a back reference.
For example, in the following lines:
caption{Some text}
caption{Some text {with braces} inside}
caption{text {with {nested} {braces} inside}}
caption{text} then more text {possibly in braces}
I'd like to match:
Some text
Some text {with braces} inside
text {with {nested} {braces} inside}
text
Initially I tried something simple:
sed 's/caption{\(.*\)}/\1/'
It's the last two that are causing problems. If I allow the expression to be greedy, the fourth one matches this:
text} then more text {possibly in braces
but if I don't then the third one matches this:
text {with {nested
Equally replacing .* with [^{]* or [^}]* isn't what I'm looking for.
It seems like I need either some kind of recursive reg exp (is that even possible?), or a way of counting the {'s and allowing that many }'s.
Any help much appreciated!
Simon