Find all strings except one string using regex

This question already has an answer here:

  • Regular expression to match a line that doesn't contain a word? 25 answers

  • ^(?!ABC$).*
    

    匹配除ABC外的所有字符串。


    Judging by you examples, I think you mean "all strings except those containing the word ABC".

    Try this:

    ^(?!.*bABCb)
    

    Invert the Match with GNU Grep

    You can simply invert the match using word boundaries and the specific string you want to reject. For example:

    $ egrep --invert-match 'bABCb' /tmp/corpus 
    "A"     --> Match
    "F"     --> Match
    "AABC"  --> Match
    "ABCC"  --> Match
    "CBA"   --> Match
    

    This works perfectly on your provided corpus. Your mileage may vary for other (or more complicated) use cases.

    链接地址: http://www.djcxy.com/p/13380.html

    上一篇: 如何否定正则表达式

    下一篇: 使用正则表达式查找除一个字符串外的所有字符串