且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式不是运算符

更新时间:2021-10-16 22:15:14

不,没有直接的not运算符.至少不是您希望的方式.

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 _\.\-:]*\)

(?!...)部分的意思是仅在以下不匹配(因此:否定)的文本跟随(因此:lookahead)匹配时匹配.但是,它不匹配实际上不会消耗与其匹配的字符(因此为零宽度).

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).

环顾四周实际上有4种组合,带有2个轴:

There are actually 4 combinations of lookarounds with 2 axes:

  • lookbehind/lookahead:指定是否考虑在该点的之前之后
  • 正/负:指定字符必须匹配还是必须匹配.
  • 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.