Perl Regex "Not" (negative lookahead)

GoldenNewby picture GoldenNewby · Apr 4, 2012 · Viewed 28.4k times · Source

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

Answer

anonymous coward picture anonymous coward · Apr 4, 2012

You can use a negative lookahead (documented under "Extended Patterns" in perlre):

/^\/(?!bob\/)/