31

I am trying to search for all occurrences of "Tom" which are not followed by "Thumb".

I have tried to look for

Tom ^((?!Thumb).)*$

but I still get the lines that match to Tom Thumb.

Tunaki
  • 116,530
  • 39
  • 281
  • 370
user1364539
  • 311
  • 1
  • 3
  • 3
  • 2
    Even in a simple case like this, it is always helpful to have some sample input and expected output. e.g., do you want to match just 'Tom' or 'Tom + everything to the end of the line'? – alan Apr 29 '12 at 19:10
  • I will try to be more explicit. I want to find any occurrence of "Tom" anywhere in a line, but I do not want to see any line that contains "Tom Thumb" – user1364539 Apr 29 '12 at 19:46

3 Answers3

34

You don't say what flavor of regex you're using, but this should work in general:

 Tom(?!\s+Thumb)
alan
  • 4,342
  • 18
  • 28
  • 1
    You may thing that it will work for ocurrences such as "TomThumb" (without spaces between them). For it to work, you will need to remove "\s+" from your regex. Example: Tom(?!Thumb) – Leonardo Montenegro Mar 03 '16 at 14:56
19

In case you are not looking for whole words, you can use the following regex:

Tom(?!.*Thumb)

If there are more words to check after a wanted match, you may use

Tom(?!.*(?:Thumb|Finger|more words here))
Tom(?!.*Thumb)(?!.*Finger)(?!.*more words here)

To make . match line breaks please refer to How do I match any character across multiple lines in a regular expression?

See this regex demo

If you are looking for whole words (i.e. a whole word Tom should only be matched if there is no whole word Thumb further to the right of it), use

\bTom\b(?!.*\bThumb\b)

See another regex demo

Note that:

  • \b - matches a leading/trailing word boundary
  • (?!.*Thumb) - is a negative lookahead that fails the match if there are any 0+ characters (depending on the engine including/excluding linebreak symbols) followed with Thumb.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
1

Tom(?!\s+Thumb) is what you search for.

noob
  • 8,053
  • 4
  • 34
  • 64