如何使用与grep -E匹配的负正则表达式?

时间:2014-05-29 16:44:50

标签: regex grep

我通过grep -E使用以下正则表达式来匹配通过|管道的特定字符串字符串。

$ git log <more switches here> | grep -E "match me"

输出:

match me once
match me twice

我真正想要的是一个否定匹配(返回所有不包含指定字符串的输出行,如下所示,但grep不喜欢它:

$ git log <more switches here> | grep -E "^match me"

期望的输出:

whatever 1
whatever 2

这是从命令行返回的完整输出:

match me once
match me twice
whatever 1
whatever 2

如何根据负正则表达式匹配得到所需的输出?

1 个答案:

答案 0 :(得分:7)

使用反转匹配的-v选项,选择不匹配的

grep -v 'match me'

另一种选择是使用-P将模式解释为Perl正则表达式。

grep -P '^((?!match me).)*$'