user1902247 user1902247 - 6 months ago 28
Java Question

Regular expression for matching double-quote but not backslash-double-quote

I need regex for matching a double quote character

but not if it is preceded by a backslash i.e.

I got to
but it's selecting two characters:
and whatever (except \ ) is in front, and I just need
character to be selected.

I need to parse the line from the stream that would look like this:
command "string1" "string2"
string can contain spaces and escaped double-quotes. I need to split it so that I get command, string1 and string2 as array.

Thanks in advance


You can use a negative look-behind: (?<!\\)".

(?<!reg1)reg2 means that reg2 must be preceeded by reg1. Note that reg1 will not be captured.

Now in Java code, your regex will look slightly different since you need to escape the double quotes and the two backslashes :

String regex = "(?<!\\\\)\"";