求一个正规表达式写法(在线等)

求一个正规表达式写法(在线等)

my $str = "abc","begin""2"",""2""end","efg";
上面的字符串,我想分割成
abc
begin""2"",""2""end
efg

现在使用my @s = split(/","/, substr($str, 1, length($str)-2);
得到的是:
abc
begin""2"
"2""end
efg

如果使用my @s = split(/","/, substr($str, 1, length($str)-2);得到的是:
ab
egin""2"",""2""en
fg

请问哪位有好地解决办法。
你可以考虑用 look ahead,在 perlre 中搜索下看看要怎么用
$_ = q{"abc","begin""2"",""2""end","efg"};

print $1, ' | ', $2, ' | ', $3 if /"(\w{3})","(.*)","(\w{3})"/;


仅针对你给出的例子啊
感谢MMMIX大师的提示,使用perlre  look ahead能得到需要的结果
my $str = "abc","begin""2"",""2""end","efg";
my @s = split(/(?<!")","(?!")/, substr($str, 1, length($str)-2));

结果:
abc
begin""2"",""2""end
efg

谢谢cobrawgl



QUOTE:
原帖由 HawaiiLeo 于 2008-12-24 12:04 发表
split(/(?<!")","(?!")/, substr($str, 1, length($str)-2));

学习
CSV文件各个项目可能包含有"和,的符号,出力的的时候把一个双引号都转换成了两个双引号,
如下的内容,怎么分割成正确的结果啊?
"abc"","",""bcd","","","123","",""

上面的内容我想得到
abc"","",""bcd


123

Excel打开,另存为*.txt
现在有两个Oracle数据库,因为其他原因不能用db link更新,也不能用固定长文件更新,
就想把数据从Oracle出力成CSV文件,然后和前一天的出力文件做差分,
再用差分结果去更新另外一个Oracle对应的表。

所以excel就不行了,而且linux环境,excel也没有啊。
我觉得你这个更适合用 parser 来做
Text::CSV 可以。http://search.cpan.org/dist/Text-CSV/lib/Text/CSV.pm

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl

use Text::CSV;
$csv = Text::CSV->new();

$line=q("abc"","",""bcd","","","123","","");

$status  = $csv->parse($line);# parse a CSV string into fields
@columns = $csv->fields();

for (@columns) {
    print $_,"\n";
}
__DATA__
"abc"","",""bcd","","","123","",""



QUOTE:
$ ./csv
abc",","bcd


123