[英]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.