用于匹配不包含单词的行的正则表达式 [英] Regex for matching lines that do NOT contain a word

查看:47
本文介绍了用于匹配不包含单词的行的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几行:

消息:Polarion 提交 Mon May 18 06:59:37 CEST 2009消息:Polarion 提交星期五 5 月 15 日 19:39:45 CEST 2009消息:424-18:使用新变量消息:Polarion 提交星期五 5 月 15 日 19:29:10 CEST 2009消息:Polarion 提交星期五 5 月 15 日 19:27:23 CEST 2009消息:000-00:做其他事情消息:Polarion 提交 2009 年 5 月 15 日星期五 17:50:30 CEST消息:401-103:添加了应用程序部分消息:Polarion 提交星期五 5 月 15 日 17:48:46 CEST 2009消息:Polarion 提交星期五 5 月 15 日 17:42:04 CEST 2009

我想得到所有不包含Polarion"的行

我该怎么做?

ps:我看到了:正则表达式匹配不是特定的东西子串但这对我没有帮助

pps:我正在尝试在 tortoiseSVN 中执行此操作以选择日志消息,并且我认为负向后视"存在问题

解决方案

这个表达式将完成这项工作.

^(?:.(?

它使用零宽度负后视断言来断言该字符串不包含Polarion".

<前>^ 锚定到字符串的开头(?: 非捕获组.匹配任何字符(?<!Polarion) 零宽度负回顾断言 - 文本到当前位置的左边不能是Polarion")* 零次或多次$ 锚定到字符串末尾

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

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

i have the following lines:

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

and i want to get all the lines NOT containing "Polarion"

how would i do this?

ps: i saw: Regex to match against something that is not a specific substring but it doesn't help me

pps: i'm trying to do this in tortoiseSVN to select log messages, and i think there is a problem with "negative lookbehind"

解决方案

This expresion will do the job.

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

It uses a zero-width negative lookbehind assertion to assert that the string does not contain "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

The following version will perform the assertion only after a 'n' - maybe this will be faster, maybe slower.

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

这篇关于用于匹配不包含单词的行的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆