我希望这个Java正则表达式在两个括号之间的匹配所有文本:显示与评论如何正确使用此Java正则表达式的负向预测?
%(.*?)\((.*?)(?!\\)\)
:
%(.*?) # match all text that immediately follows a '%'
\( # match a literal left-paren
(.*?) # match all text that immediately follows the left-paren
(?!\\) # negative lookahead for right-paren: if not preceded by slash...
\) # match a literal right-paren
但它没有(如本test证明)。
此输入:
%foo(%bar \(%baz\)) hello world)
我预计%bar \(%baz\)
但看到%bar \(%baz\
(没有逃脱右括号)。我在猜测,我对负向预测构造的使用在某种程度上是不正确的。有人可以用我的正则表达式来解释这个问题吗?谢谢。