I have this string:
goose goose goose random goose goose test goose goose gooseI'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.
2 Answers
Not sure it will work with TextMate (I do not have it, but I've tested with Notepad++).
You could try:
\b(?:(?!goose)\w)+\bExplanation:
\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 I find mistake in this expression, you can check it here:
As you see, the previous expression \b(?:(?!goose)\w)+\b doesn't find: gooses1, goose1 and all the words that start from the goose prefix. Obviously these are other words...
Correct expression (for extended words): (?<=^|\s)(?!goose(?:\s|$))\S+(?=\s|$)
You test it here:
Correct expression (for simple words): \b(?!goose\b)\w+\b
Test it:
Sincerely.
2