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

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.

Replies are listed 'Best First'.
Re^5: On zero-width negative lookahead assertions
by Roy Johnson (Monsignor) on Sep 10, 2004 at 15:51 UTC
    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.
Re^5: On zero-width negative lookahead assertions
by ysth (Canon) on Sep 10, 2004 at 17:39 UTC
    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.)