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

Discussion in 'Perl' started by GIMME, Feb 11, 2004.

  1. GIMME

    GIMME Guest

    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,"brave, 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.
    GIMME, Feb 11, 2004
    #1
    1. Advertising

  2. GIMME

    Joe Smith Guest

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

    GIMME wrote:

    > 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.


    #!/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",@fields), "\n";

    -Joe
    Joe Smith, Feb 11, 2004
    #2
    1. Advertising

  3. GIMME

    GIMME Guest

    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.Perl5Util;

    public String[] parseRecordString(String record) {
    ArrayList r = new ArrayList();
    while(! record.equals("") ) {
    record = P5UTIL.substitute("s/([^,]*?(\".*?\")*)(,|$)//",record);
    r.add(P5UTIL.group(1));
    }
    return (String[]) r.toArray(new String[r.size()]);
    }


    Joe Smith <> wrote in message news:<zhjWb.3517
    GIMME, Feb 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,830
    Hermit Dave
    Sep 22, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,452
    Victor Bazarov
    Feb 17, 2005
  3. AviraM
    Replies:
    2
    Views:
    6,326
    Manish Pandit
    Sep 28, 2006
  4. R. David Murray
    Replies:
    8
    Views:
    582
    Tim Chase
    Mar 27, 2009
  5. jOhn
    Replies:
    1
    Views:
    212
    Phlip
    Jan 29, 2008
Loading...

Share This Page