Is Java RegExp group negation possible?

I have the following regexp: (["'])(\\\1|[^\1])+\1

Obviously, it cannot be compiled because [^\1]isllall.

Is it possible to cancel the associated group?

+5
source share
1 answer

You cannot use backlinks in a positive or negative character class.

But you can achieve what you want by using negative header statements :

(["'])(?:\\.|(?!\1).)*\1

Explanation:

(["'])    # 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.
+4
source

All Articles