regex - Parsing a CSV string while ignoring commas inside the individual columns -


i trying split csv string comma delimiter.

val string ="a,b,"hi,there",c,d" 

i cannot use string.split(",") because split "hi,there" 2 different columns. can use regex solve this? came around scala-csv parser dont want use. hope there better method solve problem.i know not trivial problem. it'll helpful if people can share approaches solve problem.

use univocity-parsers csvparser instead of parsing hand. csv harder think , there many corner cases cover. found one. in short, need library read csv reliably. univocity-parsers used other scala projects (e.g. spark-csv)

i'll put example using plain java here, because don't know scala, you'll idea:

public static void main(string ... args){     csvparsersettings settings = new csvparsersettings(); //many options here, check documentation     csvparser parser = new csvparser(settings);     string[] row = parser.parseline("a,b,\"hi,there\",c,d");     for(string value : row){         system.out.println(value);     } } 

output:

a b hi,there c d 

disclosure: author of library. it's open-source , free (apache v2.0 license).


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -