Regex

    See summary

    Match everything but

    • a string starting with a specific pattern (e.g. any - empty, too - string not starting with foo):
      • Lookahead-based solution for NFAs:
        • ^(?!foo).*$
        • ^(?!foo)
      • Negated character class based solution for regex engines not supporting lookarounds:
        • ^(([^f].{2}|.[^o].|.{2}[^o]).*|.{0,2})$
        • ^([^f].{2}|.[^o].|.{2}[^o])|^.{0,2}$
    • a string ending with a specific pattern (say, no world. at the end):
      • Lookbehind-based solution:
        • (?<!world\.)$
        • ^.*(?<!world\.)$
      • Lookahead solution:
        • ^(?!.*world\.$).*
        • ^(?!.*world\.$)
      • POSIX workaround:
        • ^(.*([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.])|.{0,5})$
        • ([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.]$|^.{0,5})$
    • a string containing specific text (say, not match a string having foo):
      • Lookaround-based solution:
        • ^(?!.*foo)
        • ^(?!.*foo).*$
      • POSIX workaround: Use the online regex generator at www.formauri.es/personal/pgimeno/misc/non-match-regex
    • a string containing specific character (say, avoid matching a string having a | symbol): ^[^|]*$
    • a string equal to some string (say, not equal to foo):
      • Lookaround-based:
        • ^(?!foo$)
        • ^(?!foo$).*$
      • POSIX: ^(.{0,2}|.{4,}|[^f]..|.[^o].|..[^o])$
    • a sequence of characters:
      • PCRE (match any text but cat): /cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/i or /cat(*SKIP)(*FAIL)|(?:(?!cat).)+/is
      • Other engines allowing lookarounds: (cat)|[^c]*(?:c(?!at)[^c]*)* (or (?s)(cat)|(?:(?!cat).)*, or (cat)|[^c]+(?:c(?!at)[^c]*)*|(?:c(?!at)[^c]*)+[^c]*) and then check with language means: if Group 1 matched, it is not what we need, else, grab the match value if not empty
    • a certain single character or a set of characters: Use a negated character class: [^a-z]+ (any char other than a lowercase ASCII letter) Matching any char(s) but |: [^|]+

    source