I would expect this Java regex to match all text in between two parentheses:
%(.*?)\((.*?)(?!\\)\)
shown with comments:
%(.*?) # 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
but it does not (as demonstrated in this test).
For this input:
%foo(%bar \(%baz\)) hello world)
I expected %bar \(%baz\) but saw %bar \(%baz\ (without the escaped right-paren). I’m guessing that my usage of the negative lookahead construct is incorrect somehow. Can someone please explain the problem with my regex? Thanks.
You don’t even need a look around. Just use a negated character class
[^\\]and include it in the group: