Perl expression for parsing CSV (ignoring parsing commas when in double quotes)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GIMME

    Perl expression for parsing CSV (ignoring parsing commas when in double quotes)

    I can't figure an expression needed to parse a string.

    This problem arrises from parsing Excel csv files ...

    The expression must parse a string based upon comma delimiters,
    but if a comma appears in double quotes it should not be used for parsing.

    For example in the simple case we'd have :

    $a='hello,brave ,world';

    ($v,$x,$y) = split(/,/,$a);

    Should yeild (no suprise here)

    hello for $v
    brave for $x, and
    world for $y


    For the more advanced case we'd have :

    $a='hello,"brav e, but cruel, and joyous","planet , or world"';

    hello for $v
    brave, but cruel, and joyous for $x, and
    planet, or world for $y


    Thanks in advance to the kind souls who are sharp enough to get this one.
  • Joe Smith

    #2
    Re: Perl expression for parsing CSV (ignoring parsing commas whenin double quotes)

    GIMME wrote:
    [color=blue]
    > I can't figure an expression needed to parse a string.
    > Thanks in advance to the kind souls who are sharp enough to get this one.[/color]

    #!/bin/perl
    use Text::CSV;
    my $a = 'hello,"brave, but cruel, and joyous","planet , or world"';
    my $csv = Text::CSV->new();
    my $status = $csv->parse($a);
    my @fields = $csv->fields();
    print "a=$a status=$status fields=\n\t", join("\n\t",@fi elds), "\n";

    -Joe

    Comment

    • GIMME

      #3
      Re: Perl expression for parsing CSV (ignoring parsing commas when in double quotes)

      Thanks very much Joe.

      I didn't say it but I found a post which easily translated
      to Java and ORO ... Next time I'll look before posting.
      Haha haha.

      This

      while($_){
      s/([^,]*?(".*?")*)(,|$ )//;
      push @out, $1;
      }

      Became this ...

      import org.apache.oro. text.perl.Perl5 Util;

      public String[] parseRecordStri ng(String record) {
      ArrayList r = new ArrayList();
      while(! record.equals(" ") ) {
      record = P5UTIL.substitu te("s/([^,]*?(\".*?\")*)(, |$)//",record);
      r.add(P5UTIL.gr oup(1));
      }
      return (String[]) r.toArray(new String[r.size()]);
      }


      Joe Smith <Joe.Smith@inwa p.com> wrote in message news:<zhjWb.351 7

      Comment

      Working...