Regex for matching lines that do NOT contain a word

倾然丶 夕夏残阳落幕 提交于 2019-12-04 18:59:13

问题


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"


回答1:


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))*$



回答2:


It might be easier if you make your regular expression match the thing you are looking for and then reverse the results.

Most tools using Regex allow you to reverse the search results, generally calling the option 'v' for inVert (keeping the i for case-Insensitive):

e.g.

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

etc.




回答3:


Here's a solution using a negative lookahead, which is more widely supported than a lookbehind:

^Message:(?!Polarion).*$

(Also, since we know where Polarion might appear, we don't need to do any of the pointless fancy stuff Daniel suggested.)


Explanation of the above expression, in regex comment form, is:

(?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)



回答4:


I could not find any information about what regex engine TortoiseSVN is using, but you might ask on the mailing list. Not all engines support advanced features like zero-width negative look-behind.




回答5:


This has worked for me and is quick to enter (in a search dialogue box or the like): ^(?!.*not this).*but this




回答6:


As this answer shows, TortoiseSVN's search box isn't restricted to regular expressions. Specifically, if an expression is enclosed in !( ), it is negated and the non-matching lines show up in the result. The ! negates the enclosed expression (regular or not).

For your case,

!(Polarion)

should do the trick.



来源:https://stackoverflow.com/questions/876927/regex-for-matching-lines-that-do-not-contain-a-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!