perl在“,”上分割一个字符串

时间:2014-10-15 23:50:03

标签: regex perl split

我正在尝试从,上的CSV文件中分割几百行。 E.g:

"Acme services","Sesame street","zip","0,56","2013-10-21"  
"Black adder, intra-national Association","shaftsville rd","zap code","0,50","2014-10-14"  

我可以拆分,上的第一行,但这不适用于第二行。但是,如果我在,分裂,那么我会陷入这些情况。然后,我可以删除“使用简单的正则表达式(例如$col[i] =~ s/\"+//g

我已经尝试了@cols = split(/\",\"/,$line),我尝试了split('","',$lines)和各种变体,但每次,我都会在$line中获得完整的$col[0],{{1}空的。

任何帮助将不胜感激!感谢。

1 个答案:

答案 0 :(得分:9)

为什么不使用Text::CSV。这将处理你在值中有逗号和各种其他问题的边缘情况,

来自cpan页面

use Text::CSV;

my @rows;
my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
                or die "Cannot use CSV: ".Text::CSV->error_diag ();

open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!";
while ( my $row = $csv->getline( $fh ) ) {
    $row->[2] =~ m/pattern/ or next; # 3rd field should match
    push @rows, $row;
}
$csv->eof or $csv->error_diag();
close $fh;

$csv->eol ("\r\n");

open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!";
$csv->print ($fh, $_) for @rows;
close $fh or die "new.csv: $!";

EDIT工作示例假设两条给定的行在a.txt

use strict;
use Text::CSV;

my @rows;

my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
                or die "Cannot use CSV: ".Text::CSV->error_diag ();

open my $fh, "<:encoding(utf8)", "a.txt" or die "a.txt: $!";
while ( my $row = $csv->getline( $fh ) ) {

    foreach(@$row){
        print "$_\n";
    }
    print "\n";
}
$csv->eof or $csv->error_diag();
close $fh;

给出

Acme services
Sesame street
zip
0,56
2013-10-21

Black adder, intra-national Association
shaftsville rd
zap code
0,50
2014-10-14