正则表达式不是运算符
正则表达式中是否有 NOT 运算符? 就像该字符串: "(2001) (asdf) (dasd1123_asd 21.01.2011 zqge)(dzqge) name (20019)"
我想删除所有 \([0-9a-zA -z _\.\-:]*\)
但不是年份: <代码>(2001)。
所以正则表达式应该返回的内容必须是:(2001) name
。
注意:类似 \((?![\d]){4}[0-9a-zA-z _\.\-:]*\)
对我不起作用(< code>(20019) 不知何故也匹配...)
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 \([0-9a-zA-z _\.\-:]*\)
but not the one where it is a year: (2001)
.
So what the regex should return must be: (2001) name
.
NOTE: something like \((?![\d]){4}[0-9a-zA-z _\.\-:]*\)
does not work for me (the (20019)
somehow also matches...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不完全是这样,尽管通常您通常可以在其中一种形式
[^abc]
上使用一些解决方法,它是逐个字符而不是a
或b
> 或c
,a(?!b)
,即a
后面没有b
(?,即
b
前面没有a
Not quite, although generally you can usually use some workaround on one of the forms
[^abc]
, which is character by character nota
orb
orc
,a(?!b)
, which isa
not followed byb
(?<!a)b
, which isb
not preceeded bya
不,没有直接的 not 运算符。至少不是你希望的那样。
但是,您可以使用零宽度负前瞻:
(?!...)
部分表示“仅匹配以下文本(因此:前瞻)此 < 消耗它匹配的字符(因此:零宽度)。em>不(因此:负)与此匹配,但它实际上 一个href="http://www.regular-expressions.info/lookaround.html" rel="noreferrer">lookarounds 具有 2 个轴:
No, there's no direct not operator. At least not the way you hope for.
You can use a zero-width negative lookahead, however:
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:
您可以捕获
(2001)
部分并将其余部分替换为任何内容。.*\(([0-9]{4})\).*
表示.*
匹配任何内容\(
匹配(
字符(
开始捕获[0-9]{4}
任何单个数字四次)
结束捕获\)
匹配)
字符.*
任何内容(字符串的其余部分)You could capture the
(2001)
part and replace the rest with nothing..*\(([0-9]{4})\).*
means.*
match anything\(
match a(
character(
begin capture[0-9]{4}
any single digit four times)
end capture\)
match a)
character.*
anything (rest of string)这是一种替代方案:
使用此结构将重复模式嵌入到单个组中,其中内部组不是捕获组:
((:?pattern)*)
,这使得能够控制感兴趣的组数。然后你就可以得到你想要的:
\1\3
Here is an alternative:
Repetitive patterns are embedded in a single group with this construction, where the inner group is not a capturing one:
((:?pattern)*)
, which enable to have control on the group numbers of interrest.Then you get what you want with:
\1\3