Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How do I find matching/nesting anything?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:20 UTC ( #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 /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:

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

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[Discipulus]: infact my tk client is unable to connect at the moment
[ambrus]: choroba: that doesn't matter, the cookies are independent on the webserver, they just have to contain your username and crypted password with a seed of the user's choice, you can change the hostname or even construct a cookie without asking the server
[ambrus]: choroba: and for perlmonks (but not for everything2), you don't even need a cookie, you can just send a username and password parameter in every request, and this is even documented in What XML generators are currently available on PerlMonks?
[holli]: i admire you choroba, if i had to work at such a place, i wouldn't last long. as bosses don't like if someone calls them clueless idiots
[ambrus]: (The cookie format is not documented anywhere afaik, but it's trivial to reverse engineer even without being a pmdev.)
[holli]: or "ignorant bricks" (that is not a typo)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-10-24 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (289 votes). Check out past polls.

    Notices?