in reply to Regex negative word

/^(?!cat)\s+dog/

This is called "A zero-width negative lookahead assertion." ... See why so many people like Perl for obfuscation :)

Ref: perl.com

--
-- GhodMode
Blessed is he who has found his work; let him ask no other blessedness.
-- Thomas Carlyle

Replies are listed 'Best First'.
Re^2: Regex negative word
by Roy Johnson (Monsignor) on Mar 30, 2006 at 14:07 UTC
    Note that your example is not entirely sensible, though. The expression you give must begin with whitespace, so of course it doesn't begin cat. You have to remember that the lookahead is zero-width, so after it, you're in the same position in the string as before it. Perhaps something like
    /^(?!cat).*\sdog/
    as the most general case to consume the non-cat, before-the-dog portion of the string.

    Caution: Contents may have been coded under pressure.
      You are absolutely right! But, rereading the question, the whitespace and the dog aren't necessary, either. zer just asked for a regex that matches a string which does not begin with cat. So, I think /^(?!cat)/ should do it. That would match any line with a beginning which is not followed by a cat, right?
      --
      -- GhodMode
      
      Blessed is he who has found his work; let him ask no other blessedness.
      -- Thomas Carlyle
        Incidentally, due to my only skimming the replies, I've wasted everyone's time. My apologies to GrandFather who gave the correct answer first.
        --
        -- GhodMode
        
        Blessed is he who has found his work; let him ask no other blessedness.
        -- Thomas Carlyle