Keep It Simple, Stupid | |
PerlMonks |
How do I find matching/nesting anything?by faq_monk (Initiate) |
on Oct 08, 1999 at 00:20 UTC ( [id://594]=perlfaq nodetype: print w/replies, xml ) | Need Help?? |
Current Perl documentation can be found at perldoc.perl.org. Here is our local, out-dated (pre-5.6) version:
This isn't something that can be done in one regular expression, no matter
how complicated. To find something between two single characters, a pattern
like If you are serious about writing a parser, there are a number of modules or oddities that will make your life a lot easier. There is the CPAN module Parse::RecDescent, the standard module Text::Balanced, the byacc program, and Mark-Jason Dominus's excellent py tool at http://www.plover.com/~mjd/perl/py/ . One simple destructive, inside-out approach that you might try is to pull out the smallest nesting parts one at a time:
while (s//BEGIN((?:(?!BEGIN)(?!END).)*)END/gs) { # do something with $1 }
|
|