用于匹配不包含单词的行的正则表达式

时间:2009-05-18 09:45:32

标签: regex tortoisesvn

我有以下几行:

Message:Polarion commit Mon May 18 06:59:37 CEST 2009
Message:Polarion commit Fri May 15 19:39:45 CEST 2009
Message:424-18: use new variable
Message:Polarion commit Fri May 15 19:29:10 CEST 2009
Message:Polarion commit Fri May 15 19:27:23 CEST 2009
Message:000-00: do something else
Message:Polarion commit Fri May 15 17:50:30 CEST 2009
Message:401-103: added application part
Message:Polarion commit Fri May 15 17:48:46 CEST 2009
Message:Polarion commit Fri May 15 17:42:04 CEST 2009

我希望得到所有不包含“Polarion”的行

我该怎么做?

ps:我看到了: Regex to match against something that is not a specific substring 但它对我没有帮助

pps:我正试图在tortoiseSVN中选择日志消息,我认为“负面看后”存在问题

6 个答案:

答案 0 :(得分:19)

这个表达将完成这项工作。

^(?:.(?<!Polarion))*$

它使用零宽度负向lookbehind断言来断言该字符串不包含“Polarion”。

    ^                  Anchor to start of string
    (?:                Non-capturing group
        .              Match any character
        (?<!Polarion)  Zero-width negative lookbehind assertion - text to the
                       left of the current position must not be "Polarion"
    )
    *                  Zero or more times
    $                  Anchor to end of string

以下版本只会在'n'之后执行断言 - 也许这会更快,也许更慢。

^(?:[^n]*|n(?<!Polarion))*$

答案 1 :(得分:9)

如果您使正则表达式与您要查找的内容匹配,然后反转结果可能会更容易。

大多数使用正则表达式的工具允许您反转搜索结果,通常在 V ert中调用选项'v'(保持i为案例 - I nsensitive) :

e.g。

grep -v <search>
find /v <search>

答案 2 :(得分:3)

这是一个使用负向前瞻的解决方案,它比后视更广泛支持:

^Message:(?!Polarion).*$

(另外,既然我们知道Polarion可能出现在哪里,我们不需要做任何Daniel建议的任何毫无意义的幻想。)


正则表达式注释表中对上述表达式的解释是:

(?x)       # Enable comments
^          # Start of string (start of line in multiline mode)
Message:   # Literal text
(?!        # Begin negative lookahead
Polarion   # Literal text
)          # End negative lookahead
.*         # Greedily match any number of any character
$          # End of string (end of line in multiline mode)

答案 3 :(得分:0)

我找不到有关正在使用的正则表达式引擎TortoiseSVN的任何信息,但您可能会在mailing list上询问。并非所有引擎都支持零宽度负面后视等高级功能。

答案 4 :(得分:0)

这对我有用,可以快速输入(在搜索对话框中等):^(?!.*not this).*but this

答案 5 :(得分:0)

this answer所示,TortoiseSVN的搜索框不限于正则表达式。具体来说,如果表达式包含在!( )中,则它将被否定,并且结果中将显示不匹配的行。 !否定了所附的表达式(常规或非常规)。

对于您的情况,

!(Polarion)

应该这样做。

相关问题