Search code examples
javaregexcsv

How to split a comma separated String while ignoring escaped commas?


I need to write a extended version of the StringUtils.commaDelimitedListToStringArray function which gets an additional parameter: the escape char.

so calling my:

commaDelimitedListToStringArray("test,test\\,test\\,test,test", "\\")

should return:

["test", "test,test,test", "test"]



My current attempt is to use String.split() to split the String using regular expressions:

String[] array = str.split("[^\\\\],");

But the returned array is:

["tes", "test\,test\,tes", "test"]

Any ideas?


Solution

  • The regular expression

    [^\\],
    

    means "match a character which is not a backslash followed by a comma" - this is why patterns such as t, are matching, because t is a character which is not a backslash.

    I think you need to use some sort of negative lookbehind, to capture a , which is not preceded by a \ without capturing the preceding character, something like

    (?<!\\),
    

    (BTW, note that I have purposefully not doubly-escaped the backslashes to make this more readable)