gpt4 book ai didi

java - Java RegEx 组否定是否可能?

转载 作者:搜寻专家 更新时间:2023-10-31 20:14:52 25 4
gpt4 key购买 nike

我有以下正则表达式:(["'])(\\\1|[^\1])+\1

显然无法编译,因为[^\1]不合法。

是否可以否定匹配组?

最佳答案

不能在正字符类或负字符类中使用反向引用。

但是您可以使用负数 lookahead assertions 来实现您想要的效果:

(["'])(?:\\.|(?!\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.

关于java - Java RegEx 组否定是否可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9717972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com