我使用notepad ++搜索某些关键字(使用正则表达式)。类似的东西(word1 | word2 |这个陈述|另一个陈述)。它有效,但我可以搜索并显示除某个关键字以外的所有结果吗?类似的东西排除下面的单词(排除这个|排除这个)?例如,下面。
samedir \ File1.log
This is line 1
This is line 2
This is line 3
exclude this
This is line 4
This is line 5
This is line 6
not excluded
excluding this
samedir \ File2.log
This is line 1 1
This is line 2 1
This is line 3 1
exclude this
This is line 4 1
This is line 5 1 1
This is line 6 1
not excluded
excluding this
例如:我想在两个文件中启动查找(在同一目录中),但排除包含excluding this
和exclude this
的行
结果应该显示如下
File1.log
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
not excluded
File2.log
This is line 1 1
This is line 2 1
This is line 3 1
This is line 4 1
This is line 5 1 1
This is line 6 1
not excluded
答案 0 :(得分:1)
你可以用先行断言来做到这一点:
^(?!excluding this|exclude this)[^\r\n]*$
只要它们不包含excluding this
或exclude this
,就会匹配整行。
关键是(?!)
部分。有关详细信息,请参阅http://www.regular-expressions.info/lookaround.html。
答案 1 :(得分:1)
您可以尝试使用下面的正则表达式匹配所有不具有exclude
或excluding this
字符串的行。
^(?!.*\bexclud(?:ing|e) this\b).+$
开始时此(?!.*\bexclud(?:ing|e) this\b)
否定前瞻声明我们要匹配的行中不存在字符串exclude this
或excluding this
。也就是说,上述正则表达式将匹配除包含exclude this
或excluding this
答案 2 :(得分:0)
我想排除一个字符串并在一行中搜索两个字符串,所以我使用了这个正则表达式: ^(?!.*excludethisstring).*includethisstring1.includethisstring2.$
这将使搜索的一行必须包含两个字符串,如果您想搜索其中任何一行: ^(?!.excludethisstring).(includethisstring1|includethisstring2).*$