1

I have a CSV file that I'm attempting to parse using regex. The file is controlled in terms of delimiters, carriage returns, etc.

I have the following to split each row into an array of values.

/('[^']+'|[^,]+)/g

There's 2 issues with this pattern that I'm attempting to work out the kinks. The first is that it breaks commas that are used within an answer.

The second, and main issue that I'm trying to figure out is how to change this pattern to allow the null values to still be reflected in the output array.

Test Input

,,,'This is a test', 'I'm a test as well', 'I,too, am a test'

Desired Output

null
null
null
This is a test
I'm a test as well
I,too, am a test

Currently I'm getting

This is a test
I'm a test as well
I
too
am a test
JamesEggers
  • 12,885
  • 14
  • 59
  • 86
  • 1
    Which language are you using? – devnull Mar 17 '14 at 16:31
  • Currently - JavaScript/Nodejs for prototyping. The pattern and overall logic of the project could be ported to C# eventually. – JamesEggers Mar 17 '14 at 16:34
  • 3
    The main problem is that literal quotes inside strings are not escaped. – Casimir et Hippolyte Mar 17 '14 at 16:38
  • 1
    Doubt this is possible via pure RegExp, especially without the ability to use a lookbehind (JS), due to nested commas and single quotes. You should look to try doing this programmatically by writing a mini parser of sorts. – tenub Mar 17 '14 at 16:38
  • You may find my answer to a similar question to be helpful: [How can I parse a CSV string with Javascript?](http://stackoverflow.com/a/8497474/433790) – ridgerunner Mar 17 '14 at 18:23

1 Answers1

0

This will match exactly what you want it too match

(?:,|^)    # start with start of line or comma
(\s*       # start capture group
 (?:
  [^',].*? # match anything not starting with a comma or a quote
  |        # OR 
  '.*?'\s* # match anything starting and ending with quotes
 )?        # OR nothing at all 
)
(?=,|$)    # end with , or end of string, but don't match

see it in action.

Lodewijk Bogaards
  • 19,777
  • 3
  • 28
  • 52