正则表达式不是运算符 [英] Regex not operator

查看:118
本文介绍了正则表达式不是运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正则表达式中是否存在NOT运算符? 就像在该字符串中一样:"(2001) (asdf) (dasd1123_asd 21.01.2011 zqge)(dzqge) name (20019)"

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

我要删除所有\([0-9a-zA-z _\.\-:]*\),而不要删除一年中的一个:(2001).

I want to delete all \([0-9a-zA-z _\.\-:]*\) but not the one where it is a year: (2001).

因此正则表达式应返回的内容必须是:(2001) name.

So what the regex should return must be: (2001) name.

注意:\((?![\d]){4}[0-9a-zA-z _\.\-:]*\)之类的东西对我不起作用((20019)在某种程度上也匹配...)

NOTE: something like \((?![\d]){4}[0-9a-zA-z _\.\-:]*\) does not work for me (the (20019) somehow also matches...)

推荐答案

不,没有直接的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.

这篇关于正则表达式不是运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆