在 TextMate 中反转正则表达式匹配

luc*_*ano 1 regex textmate

我有这个字符串:

goose goose goose random goose goose test goose goose goose
Run Code Online (Sandbox Code Playgroud)

我在 TextMate 中使用正则表达式来查找任何不是goose. 因此randomtest

所以我尝试了这个正则表达式:

[^\sgoose\s]
Run Code Online (Sandbox Code Playgroud)

但这并不是我想要的。它匹配任何不是 aspace或字母的字符g o s e

我怎样才能找到正则表达式来匹配任何不是的整个单词goose?因此,应该有 2 个匹配项randomtest

Tot*_*oto 5

不确定它是否适用于 TextMate(我没有,但我已经用 Notepad++ 进行了测试)。

你可以试试:

\b(?:(?!goose)\w)+\b
Run Code Online (Sandbox Code Playgroud)

解释:

\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
Run Code Online (Sandbox Code Playgroud)