Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Don't ask to ask, just ask
 
PerlMonks  

Re: regex: negative lookahead

by kennethk (Monsignor)
on Dec 05, 2011 at 15:54 UTC ( #941884=note: print w/ replies, xml ) Need Help??


in reply to regex: negative lookahead

If you take a look at what you have in $1 after your failed miss for 2, the issue will become more apparent, I think; print $1; yields log4j.rootLogger=INFO, FILE, SYSLOG. Essentially, you have already consumed ", SYSLOG" by the time you get to the end of the string, so clearly that bit does not follow. A negative look-behind will actually yield what you intended:

print "has_matched $has\n" if $has =~ /^($start(\w+(,\s)?)+)(?<!$end)$/;


Comment on Re: regex: negative lookahead
Select or Download Code
Re^2: regex: negative lookahead
by svenXY (Deacon) on Dec 05, 2011 at 16:08 UTC
    Hi,
    ++kennethk - thanks for clarifying. I'm still a little bit confused by the difference between (?!...) and (?<!...) though.
    Regards,
    svenXY
      The difference is a lookahead versus a lookbehind. If you ignore the lookahead/lookbehind because they are zero width (don't consume characters), you would expect /^($start(\w+(,\s)?)+)$/ to grab an entire string between start and finish. When you get to the part of the regular expression where you have your assertion, the cursor is here:
      log4j.rootLogger=INFO, FILE, SYSLOG ^
      If you look ahead of this position, there is nothing, which clearly passes the negative lookahead check. The offending string is behind your position, thus you need a negative lookbehind.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://941884]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-04-18 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (461 votes), past polls