Invert regular expression match in TextMate

regextextmate

I have this string:

goose goose goose random goose goose test goose goose goose

I'm using regular expression in TextMate to find any word that isn't goose. therefore random and test.

So I tried this regular expression:

[^\sgoose\s]

But this isn't quite doing what I want. It's matching any character that isn't a space or letters g o s e.

How can I find get the regular expression to match any whole word that isn't goose? Therefore, there should be 2 matches random and test.

Best Answer

Not sure it will work with TextMate (I do not have it, but I've tested with Notepad++).

You could try:

\b(?:(?!goose)\w)+\b

Explanation:

\b          : word boundary
(?:         : start non capture group
  (?!goose) : negative lookahead, make sure we don't have the word "goose"
  \w        : a word character, you may use "[a-zA-Z]" for letters only or "." for any character but newline
)+          : group may appears 1 or more times
\b          : word boundary
Related Question