Regex not operator

前端 未结 3 572
刺人心
刺人心 2020-11-28 22:13

Is there an NOT operator in Regexes? Like in that string : \"(2001) (asdf) (dasd1123_asd 21.01.2011 zqge)(dzqge) name (20019)\"

I want to delete all

3条回答
  •  执念已碎
    2020-11-28 22:24

    No, there's no direct not operator. At least not the way you hope for.

    You can use a zero-width negative lookahead, however:

    \((?!2001)[0-9a-zA-z _\.\-:]*\)
    

    The (?!...) part means "only match if the text following (hence: lookahead) this doesn't (hence: negative) match this. But it doesn't actually consume the characters it matches (hence: zero-width).

    There are actually 4 combinations of lookarounds with 2 axes:

    • lookbehind / lookahead : specifies if the characters before or after the point are considered
    • positive / negative : specifies if the characters must match or must not match.

提交回复
热议问题