Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string on commas not contained within double-quotes with a twist

Tags:

java

regex

split

I asked this question earlier and it was closed because it was a duplicate, which I accept and actually found the answer in the question Java: splitting a comma-separated string but ignoring commas in quotes, so thanks to whoever posted it.

But I've since run into another issue. Apparently what I need to do is use "," as my delimiter when there are zero or an even number of double-quotes, but also ignore any "," contained in brackets.

So the following:

"Thanks,", "in advance,", "for("the", "help")"

Would tokenize as:

  • Thanks,
  • in advance,
  • for("the", "help")

I'm not sure if there's anyway to modify the current regex I'm using to allow for this, but any guidance would be appreciated.

line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
like image 518
binarymelon Avatar asked Feb 22 '10 18:02

binarymelon


People also ask

How do you split a string with double quotes?

Use method String. split() It returns an array of String, splitted by the character you specified.

How do you split a string after a comma?

To split a string with comma, use the split() method in Java. str. split("[,]", 0);

How do you ignore a comma in a string in python?

replace your string. split(",") by string. split(", ") with a space after the comma. This should be enough to avoid splitting the numbers.


1 Answers

Sometimes it is easier to match what you want instead of what you don't want:

String s = "\"Thanks,\", \"in advance,\", \"for(\"the\", \"help\")\"";
String regex = "\"(\\([^)]*\\)|[^\"])*\"";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while(m.find()) {
    System.out.println(s.substring(m.start(),m.end()));
}

Output:

"Thanks,"
"in advance,"
"for("the", "help")"

If you also need it to ignore closing brackets inside the quotes sections that are inside the brackets, then you need this:

 String regex = "\"(\\((\"[^\"]*\"|[^)])*\\)|[^\"])*\"";

An example of a string which needs this second, more complex version is:

 "foo","bar","baz(":-)",":-o")"

Output:

"foo"
"bar"
"baz(":-)",":-o")"

However, I'd advise you to change your data format if at all possible. This would be a lot easier if you used a standard format like XML to store your tokens.

like image 129
Mark Byers Avatar answered Sep 28 '22 09:09

Mark Byers