Home > Software design >  Split String with special characters in Java
Split String with special characters in Java

Time:10-07

I have a String with many commas for example:

1,2,3,4,5,"one, two", 6, 7, "three, four", 8, 9

I want to split this string by comma(,) but parts with quotes shouldn't be splitted. Is there a simple way to do this or should I prepare this string for example replace comma with something else:

1,2,3,4,5,"one#COMMA# two", 6, 7, "three#COMMA# four", 8, 9

and then split by comma(,)

CodePudding user response:

You can split by , followed by an optional blank characters \s* with a negative lookahead. This Regex would identify the commas qualified to split the input String.

,(?![^,] "\s*,)\s*

The explanation is below and the demo is here: https://regex101.com/r/ghNMVo/1:

  • , is a comma literally
  • (?![^,] "\s*,) is a negative lookahead that matches if the following characters listed between (?!...) are not found right after the comma (,). This basically identifies the end of a String followed by the comma (ex. ...four",) which means, the previous comma is not matched for split as long as a String follows.
    • [^,] at least one character except for the comma itself
    • " followed by the " matched literally
    • \s* any number of blank characters
    • , followed by the , matched literally
  • \s* are optional blank characters

When applied to Java, don't forget to escape the characters.

final String string = "1,2,3,4,5,\"one, two\", 6, 7, \"three, four\", 8, 9";
final String[] array = string.split(",(?![^,] \"\\s*,)\\s*");
Arrays.asList(array).forEach(System.out::println);
1
2
3
4
5
"one, two"
6
7
"three, four"
8
9
  • Related