0

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

user1902247
  • 123
  • 12
  • Since quotes are Java string delimiters and backslash is a regex metacharacter, your question could have several interpretations. I edited to be clearer according to what I _think_ you meant. Please revert the edit if this is not correct. – Jim Garrison Aug 06 '16 at 22:29
  • 2
    Note also that this really smells like an [XY Problem](http://xyproblem.info). I'd guess you are trying to parse a double-quote delimited string that can contain escaped double quotes. Regexes are not a good idea for this task as the grammar is not regular. Some background on what you're _really_ trying to accomplish would help. – Jim Garrison Aug 06 '16 at 22:31
  • You are right, I edited my question – user1902247 Aug 06 '16 at 22:40
  • Can your text also contain ``\``? If yes should this ``\`` be also escaped with ``\\`` like ``\"`` is? If yes then you have potential problem because for data like `c:\\path\\"foo\"bar"` first `"` is not escaped but has escaped ``\\`` before it, only second `"` is escaped. Regex doesn't look like right tool here, it looks like job for parser. – Pshemo Aug 06 '16 at 23:39
  • Maybe this will interest you: [How to parse command line arguments in Java?](http://stackoverflow.com/questions/367706/how-to-parse-command-line-arguments-in-java) – Pshemo Aug 06 '16 at 23:43

2 Answers2

4

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 = "(?<!\\\\)\"";
Dici
  • 25,226
  • 7
  • 41
  • 82
2

You can use a negative lookbehind for this: match " not preceded by a \\, for example:

Pattern pat = Pattern.compile("(?<!\\\\)\"");

System.out.println(pat.matcher("quote \" not escaped").find());
// prints true, the " doesn't follow a \

System.out.println(pat.matcher("quote \\\" escaped").find());
// prints false, the " follows a \
janos
  • 120,954
  • 29
  • 226
  • 236