Regex for all strings not containing a string?

This question already has an answer here:

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

  • You can use negative lookbehind, eg:

    .*(?<!.config)$
    

    This matches all strings except those that end with ".config"


    Your question contains two questions, so here are a few answers.

    Match lines that don't contain a certain string (say .config ) at all:

    ^(?:(?!.config).)*$r?n?
    

    Match lines that don't end in a certain string:

    ^.*(?<!.config)$r?n?
    

    and, as a bonus: Match lines that don't start with a certain string:

    ^(?!.config).*$r?n?
    

    (each time including newline characters, if present.

    Oh, and to answer why your version doesn't work: [^abc] means "any one (1) character except a, b, or c". Your other solution would also fail on test.hg (because it also ends in the letter g - your regex looks at each character individually instead of the entire .config string. That's why you need lookaround to handle this.


    (?<!.config)$
    

    :)

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

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

    下一篇: 所有不包含字符串的字符串的正则表达式?