I'm a bit confused. I have this csv line with double quote("") as grouped strings, and want to convert to Kotlin List. However it produces a single array with size 1. I want to be able to get the group, assumes at position 2.
val s = "John Doe, 13, \"Subject 1, Subject 2, Subject 3\""
var list: List<String> = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")
Log.d("size:", list.size.toString() + " - subjects:" + list[2])
This gives me error
java.lang.IndexOutOfBoundsException: Index: 2, Size: 1
The given regex just works fine. Currently you try to split the string s
at the raw regex as delimeter, which does not exists in s
. Simply add .toRegex()
to the regex.
val s = "John Doe, 13, \"Subject 1, Subject 2, Subject 3\""
var list: List<String> = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)".toRegex())
Log.d("size:", list.size.toString() + " - subjects:" + list[2])
I'd recommend not doing the parsing yourself, but using an existing library. (For example, I found Apache Commons CSV easy to use from Kotlin.)
Although writing parsing code can be fun, and CSV seems simple enough, it has enough complications and variations that unless you created it yourself, you're likely to miss some cases. (Not just escaped quotes, but other escaped characters, nested quotes, fields which include newlines, comment lines… And my favourite gotcha: MS Excel uses the machine's list separator, which can be semicolon or another character instead of comma to separate fields!)
Trust me, I've been there…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With