CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Frequently Asked Questions:
How do I find matching/nesting anything?

 



Jasmine
Administrator

Jan 19, 2001, 2:46 PM

Post #1 of 1 (1640 views)
How do I find matching/nesting anything? Can't Post

(From the Perl FAQ)

How do I find matching/nesting anything?

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 /x([^x]*)x/ will get the intervening bits in $1. For multiple ones, then something more like /alpha(.*?)omega/ would be needed. But none of these deals with nested patterns, nor can they. For that you'll have to write a parser.

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:

Code
    while (s//BEGIN((?:(?!BEGIN)(?!END).)*)END/gs) { 
# do something with $1
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives