Archive › December, 2009

One-Line CSV Parser for ActionScript, AS3, Flex, AIR

Well, okay, it’s slightly more than a line, but still I wish someone had written a blog post like this when I went out looking for a CSV parser for YNAB 3.

Thanks to comments in this blog, I discovered that you can split a CSV line, and correctly account for escaped commas, doublequotes, etc, with the following expression:

For each line of text in your CSV file, just do this:
[code lang="as3"]
var components : Array = line.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/g);
[/code]

There are some side effects, like quotes that are used to wrap values are still included in the components, but it was a small price to pay. I think it beats the typical state machine approach. (Well, I say that cause I didn’t have to come up with that beast of a Regular Expression).

Now you’ve got your array of values for your csv line. I’m sure you can take it from here.

Comments ( 4 )