Pergunta

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.

Foi útil?

Solução

You don't even need a look around. Just use a negated character class [^\\] and include it in the group:

%(.*?)\((.*?[^\\])\)

Outras dicas

I figured out the problem. I was using negative lookahead when I actually needed negative lookbehind.

The regex should've been:

%(.*?)      # match all text that immediately follows a '%'
\(          # match a literal left-paren
(.*?)       # match all text that immediately follows the left-paren
(?<!\\)     # negative lookbehind for right-paren: if not preceded by slash...
\)          # match a literal right-paren

This fix is demonstrated here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top