How can I parse quoted CSV in Perl with a regex?

前端 未结 7 1446
青春惊慌失措
青春惊慌失措 2020-11-30 09:10

I\'m having some issues with parsing CSV data with quotes. My main problem is with quotes within a field. In the following example lines 1 - 4 work correctly but 5,6 and 7 d

相关标签:
7条回答
  • 2020-11-30 10:02

    tested; working:-

    $_.=','; # fake an ending delimiter
    
    while($_=~/"((?:""|[^"])*)",|([^,]*),/g) {
      $cell=defined($1) ? $1:$2; $cell=~s/""/"/g; 
      print "$cell\n";
    }
    
    # The regexp strategy is as follows:
    # First - we attempt a match on any quoted part starting the CSV line:-
    #  "((?:""|[^"])*)",
    # It must start with a quote, and end with a quote followed by a comma, and is allowed to contain either doublequotes - "" - or anything except a sinlge quote [^"] - this goes into $1
    # If we can't match that, we accept anything up to the next comma instead, & put it into $2
    # Lastly, we convert "" to " and print out the cell.
    

    be warned that CSV files can contain cells with embedded newlines inside the quotes, so you'll need to do this if reading the data in line-at-a-time:

    if("$pre$_"=~/,"[^,]*\z/) {
      $pre.=$_; next;
    }
    $_="$pre$_";
    
    0 讨论(0)
提交回复
热议问题