Perl Regex "Not" (negative lookahead)

32,644

TLDR: Negative Lookaheads

If you wanted a negative lookahead just to find "foo" when it isn't followed by "bar"...

$string =~ m/foo(?!bar)/g;

Working Demo Online

Source

To quote the docs...

(?!pattern)

(*nla:pattern)

#(*negative_lookahead:pattern)

A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of "foo" that isn't followed by "bar". Note however that lookahead and lookbehind are NOT the same thing. You cannot use this for lookbehind. (Source: PerlDocs.)

Negative Lookaheads For Your Case

The accepted answer is great, but it leaves no explanation, so let me add one...

/^\/(?!bob\/)/
  • ^ — Match only the start of strings.
  • \/ — Match the / char, which we need to escape because it is a character in the regex format (i.e. s/find/replacewith/, etc.).
  • (?!...) — Do not match if the match is followed by ....
  • bob\/ — This is the ... value, don't match bob/', once more, we need to escape the /`.
Share:
32,644
GoldenNewby
Author by

GoldenNewby

Updated on July 06, 2021

Comments

  • GoldenNewby
    GoldenNewby almost 3 years

    I'm not terribly certain what the correct wording for this type of regex would be, but basically what I'm trying to do is match any string that starts with "/" but is not followed by "bob/", as an example.

    So these would match:

    /tom/
    /tim/
    /steve
    

    But these would not

    tom
    tim
    /bob/
    

    I'm sure the answer is terribly simple, but I had a difficult time searching for "regex not" anywhere. I'm sure there is a fancier word for what I want that would pull good results, but I'm not sure what it would be.

    Edit: I've changed the title to indicate the correct name for what I was looking for