Search code examples
regexstringkotlinsplitregexp-replace

Split a string by Comma by excluding the ones inside Double Quotes


I have this string in particular:

123,abc,true,true,true,false,true,false,false,false,123,"my name is : something('Jack'), email is 'abc@gmail.com' is already provided. The company's policy 'xyz', is beneficial.",ABCD,591633,1

I want to split the string by commas by excluding the commas inside the Double Quotes. So the Output that I am trying to produce here is

[123, abc, true, true, true, false, true, false, false, false, 123, "my name is : something('Jack') email is 'abc@gmail.com' is already provided. The company's policy 'xyz' is beneficial.", ABCD, 591633, 1]

I have tried using other stackoverflow post's answers, but they DID NOT work for such string. Can anyone help me on how I can achieve this?

I'm trying to implement this in Kotlin.

One way I could think to get this is, first getting rid of commas inside the double quotes and then splitting the string by commas, but couldn't get the expected result.


Solution

  • Since you're trying to parse a CSV file, it's almost always far better to use an existing library than to write your own code.  Advantages include:

    • It copes with all the corner cases and subtleties that you don't have the time to learn about or support.  For example, what happens if the double-quote characters are themselves quoted?  What if the delimiter is a semicolon or tab or space or something other than a comma?  (Yes, that still gets called CSV, confusingly enough.  For example, Excel writes semicolons in some locales that have comma as the decimal separator.)  What about headers, line-breaks, comment lines, blank lines, escape sequences?  And can you handle and test all the different dialects of CSV that different programs write?
    • It has been thoroughly tested, and proved by countless users.
    • It's updated when standards change, bugs are found, or performance can be improved.
    • It's commonly used so other developers may be familiar with it.

    One example is Apache Commons CSV, which is free, open-source, and trivially easy to include in any Maven or Gradle project.