in reply to CRLF in a csv

/(".*?"|\n)/gms;
That won't work with the text "John said \"Hello\"". Text::ParseWords can handle this.

Replies are listed 'Best First'.
Re: Re: CRLF in a csv
by geofisch (Initiate) on Apr 04, 2001 at 23:31 UTC
    You're right, of course. This seems to be better:
    use strict; use Text::ParseWords; undef $/; my $infile = (<>); my @fields = &parse_line("\n", 1, $infile); foreach (@fields) { s/\n/ /; print "$_\n"; }
    It handles "John said \"Hello\"". As a matter of fact, it handles it better than Excel, which creates a field that looks like
    John said \Hello\""
      But, then again... The Text::ParseWords module is certainly powerful, but I can't seem to make it handle errors very well. It does not like
      "John said "Hello"","again"
      So I went back to my earlier method with a better regex
      undef $/; my $infile = (<>); my @fields = $infile =~ /(".*?"(?=,|\n)|\n)/gms; my $place = "\n"; foreach (@fields) { if ($place ne "\n" && $_ ne "\n") {print ",";}; unless ($_ eq "\n") {s/\n//g;}; print "$_"; $place = $_; }