Friday, 9 August 2013

Regex to split a CSV

Regex to split a CSV

I know this (or similar) has been asked many times but having tried out
numerous possibilities I've not been able to find a a regex that works
100%.
I've got a CSV file and I'm trying to split it into an array, but
encountering two problems: quoted commas and empty elements.
The CSV looks like: 123,2.99,AMO024,Title,"Description, more info",,123987564
The regex I've tried to use is:
thisLine.split(/,(?=(?:[^\"]\"[^\"]\")(?![^\"]\"))/)
The only problem is that in my output array the 5th element comes out as
123987564 and not an empty string.

No comments:

Post a Comment