It should work like this:
"(?:\\.|[^"])+"
without lookahead/behind stuff.
This does the following:
- Look for a
", consume it
- Check if the next 2 characters are a backslash followed by any character (this will match two backslashes
\\, where the first is masking the second, and \" as well). If that can not be found, go to Step 3. If found, consume those 2 characters and repeat Step 2.
- Check if the next character is not a
". If so, consume and go to step 2. If not (it IS a "), go to Step 4
- Consume the
" which must be here
As HamZa pointed out, this Regex will fail if a " is found outside of a string and not intended to be a start of a string. E.g. for Java Code this is the case if you have something like
Character c = '\"'
(" as a char) or
if (foo) { /* chosen "sometimes */ String g = "bar"; }
(random " inside a comment)