#! /usr/bin/perl # use strict; while () { chomp; # avoid matching against newline my $words = join "=>", parse_csv($_); print "$words\n"; } sub parse_csv { my $text = shift; # record containing comma-separated values my @new = ( ); push @new, $+ while $text =~ m{ \s*"([^\"\\]*(?:\\.[^\"\\]*)*)"\s*,? # Matches a phrase that may contain commas | \s*([^,]+)\s*,? # Something that is not a comma | \s*, # Just a comma - no data }gx; push (@new, undef) if substr($text, -1,1)eq ','; return @new; } __DATA__ 1,the,simple,case "with","quoted" , "strings that contain spaces" "with","quoted" , "comma, internally" "with","quoted" , "comma, internally, with null data",,,