Java RegEx find, except when between quotes

Posted by user1833511 on Stack Overflow See other posts from Stack Overflow or by user1833511
Published on 2013-11-04T15:37:15Z Indexed on 2013/11/04 15:53 UTC
Read the original article Hit count: 428

Filed under:
|
|
|

I need a Java RegEx to split, or find something in a string, but exclude stuff that's between double quotes. What I do now is this:

String withoutQuotes = str.replaceAll("\\\".*?\\\"", "placeholder");
withoutQuotes = withoutQuotes.replaceAll(" ","");

but this doesn't work nice with indexOf, and I also need to be able to split, for example:

String str = "hello;world;how;\"are;you?\""
String[] strArray = str.split(/*some regex*/);
// strArray now contains: ["hello", "world", "how", "\"are you?\"]
  • quotes are always balanced
  • quotes can be escaped with \"

Any help is appreciated

© Stack Overflow or respective owner

Related posts about java

Related posts about regex