33

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

2
  • 1
    what about /jimbob/? what about /bob/apples?
    – ysth
    Apr 4, 2012 at 2:58
  • For my purposes /jimbob/ was okay, but /bob/apples was not Apr 4, 2012 at 22:58

2 Answers 2

35

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

/^\/(?!bob\/)/
5
  • Probably /^\/(?!bob\/)/ , though-- but you got the gist of it. Thanks! Apr 4, 2012 at 0:25
  • @GoldenNewby Woops, overlooked that part of the question
    – user47322
    Apr 4, 2012 at 0:25
  • 2
    With default regex flags now a feature, that ^ is better written as \A. With alternate delimiters and /x, it looks like m| \A / (?!bob/) |x. Apr 4, 2012 at 3:41
  • 9
    @brian d foy, It's preposterous to suggest the existence this feature means /^/ has to be changed to /\A/, /$/ to /(?=\n?\z)/, /./ to /[^\n]/ and / / to /[ ]/.
    – ikegami
    Apr 4, 2012 at 7:45
  • @ikegami: $ should be \z more often than not anyway; I am not without appreciation for a reason to use the parallel \A
    – ysth
    Apr 5, 2012 at 5:05
8

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 /`.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.