正则表达式查找包含一个单词但没有另一个单词的行

regex to find a line that has a word without another word

我有以下几行

test
words
more words test
&test
words and test

我正在使用 Notepad++,我想要一个正则表达式来查找包含单词 "test" 的行,如果它在该行中没有符号“&”.. 所以在上面的示例中它应该找到第 1、3、5 行 - 但不是第 4 行,该行中有“&”。

我试过了

[&]test[^\&]‎

^(?=.*test)(?!.*(?:&))

但是没用。

您可以使用这个基于负前瞻的正则表达式:

^(?!.*&).*\btest\b

RegEx Demo

(?!.*&) 将确保该行中没有 &。确保使用 MULTILINE 模式。

查找内容:

^[^&\n]*test[^&\n]*$

使用 negated character class 允许除 & 和换行符之外的任何字符。