Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: On zero-width negative lookahead assertions

by Eimi Metamorphoumai (Deacon)
on Sep 10, 2004 at 14:46 UTC ( [id://390097]=note: print w/replies, xml ) Need Help??


in reply to Re^2: On zero-width negative lookahead assertions
in thread On zero-width negative lookahead assertions

The regexp engine will match if it can find any way to. So what you're asking for is "root, followed by some number (possibly zero) of whitespace characters, followed by something that is not 'admin@somewhere.here'". So it matches with root, followed by zero spaces, followed by ' admin@somewhere.here' (with a leading space). Since the string ' admin@somewhere.here' isn't 'admin@somewhere.here' (without the space), the lookahead works. That's why you need the \s* inside the lookahead, making it "try to find spaces followed by admin@somewhere.here, and if you can, fail" instead of "look for spaces, but make sure it's not followed by admin@somewhere.here". Subtle, but important.

Replies are listed 'Best First'.
Re^4: On zero-width negative lookahead assertions
by Crian (Curate) on Sep 10, 2004 at 14:54 UTC
    not exactly, not

    "followed by something that is not 'admin@somewhere.here'"

    it is

    "not followed by 'admin@somewhere.here'

    That is a difference, because it matches, if nothing follows at all.
Re^4: On zero-width negative lookahead assertions
by bronto (Priest) on Sep 10, 2004 at 15:32 UTC

    Uhmmmmm... so the old adagio that "* is greedy" has an exception when zwnlaa come into play; I expected that the \s* had eat all the whitespace before the e-mail address. Ok. Now I am still to understand why that \S thing works...

    Oh, by the way, I am doing:

    perl -i.bak -pe 'BEGIN { $status = 0 } /^root:(?!\s*admin\@somewhere\.here\s*$)/ and $status = 1 ; END { exit $status }' aliases

    and it seems to work great!

    Ciao!
    --bronto


    In theory, there is no difference between theory and practice. In practice, there is.
      so the old adagio that "* is greedy" has an exception
      No, it is always greedy, but its greed is not absolute. It will eat as much as it can, but if that results in failure to match, then it will relinquish some of what it ate (try not to picture that) to allow the whole expression to match. Greed (and the anti-greed of minimal-matching) is tempered by persistence in regexen.

      Recently, hv wrote a tutorial explaining the rules the regex engine uses in trying to find a match.


      Caution: Contents may have been coded under pressure.
      To make it behave as you describe, use (?>\s*). The (?> ) says whatever is in it will match whatever it would match at that point in the string as an independent expression. So if matching all the spaces makes something later on fail, it won't backtrack and try having the \s* match fewer spaces.

      (It's really time to unmark all of the extensions as experimental, except perhaps how variables in (?{}) and (??{}) bind.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found