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

Re: lookahead, lookbehind, ... I'm lost

by Roy Johnson (Monsignor)
on May 06, 2009 at 18:13 UTC ( #762349=note: print w/replies, xml ) Need Help??

in reply to lookahead, lookbehind, ... I'm lost

If you don't mind my whoring my own tutorial (you may have read it), the idiom you're looking for is:

Matching a pattern that doesn't include another pattern

You might want to capture everything between foo and bar that doesn't include baz. The technique is to have the regex engine look-ahead at every character to ensure that it isn't the beginning of the undesired pattern:
/foo # Match starting at foo ( # Capture (?: # Complex expression: (?!baz) # make sure we're not at the beginning of baz . # accept any character )* # any number of times ) # End capture bar # and ending at bar /x;
Note that you have to have the lookahead checked for every character you're accepting. In your case, the [^\n]+ sub-pattern needs to be adjusted to make sure every character is not the beginning of a Lib:
(?: # Complex expression: (?!Lib) # make sure we're not at the beginning of Lib [^\n] # accept any character )+ # any positive number of times

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: lookahead, lookbehind, ... I'm lost
by ExReg (Curate) on May 07, 2009 at 16:42 UTC
    Great tutorial! Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://762349]
[Corion]: Heh - I just realized, $work management just declared "no raises for anybody this year", and I'm not even angry about that. Even though that implicitly means "raises for the people with an automatic 5% raise in their contract", which the newly-merged ...
[Corion]: ... coworkers have. But I guess I've gone more mellow since I get to relax more, and such stuff doesn't make me as angry as it used to.
[Corion]: $boss will still get to listen to my interpretation :-D
[Eily]: hey, I'm just behind Larry in SioB \o/

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-01-22 11:02 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.