[英]Is Java RegExp group negation possible?
我有以下正则表达式: (["'])(\\\\\\1|[^\\1])+\\1
显然它无法编译,因为[^\\1]
是非法的。
是否有可能否定一个匹配的群体?
您不能在正面或负面角色类中使用反向引用。
但是你可以使用负前瞻断言来实现你想要的东西:
(["'])(?:\\.|(?!\1).)*\1
说明:
(["']) # Match and remember a quote.
(?: # Either match...
\\. # an escaped character
| # or
(?!\1) # (unless that character is identical to the quote character in \1)
. # any character
)* # any number of times.
\1 # Match the corresponding quote.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.