Just another Perl shrine | |
PerlMonks |
Re: Parsing nested parenthesesby Abigail-II (Bishop) |
on Nov 18, 2003 at 19:55 UTC ( [id://308100]=note: print w/replies, xml ) | Need Help?? |
Once you know how to write a regex to match a string with
balanced parens, it's not to hard to write a regex to do what you
want.
How does a string with balanced parens look like? It starts with an opening paren, then "junk" separated by strings with balanced parens, where "junk" is a string consisting of something that isn't a paren. Quite like a delimited string with escaped delimiters actually. Now, how do we go from string with balanced parens to the requested string? Simple. We start with an opening paren, then junk string separated by strings with balanced parens, then "C", then junk strings separated by strings with balanced parens, and finally a closing paren. So much for text. Here's the code:
(I cooked up this regex while I was walking home from the train station. It turned out to work at the first try). Abigail
In Section
Seekers of Perl Wisdom
|
|