Chinaunix

标题: 字符串分割问题 [打印本页]

作者: yizhengming    时间: 2012-11-19 20:38
标题: 字符串分割问题
本帖最后由 yizhengming 于 2012-11-19 20:48 编辑

按逗号分割,双引号里面的逗号不作为分隔符, 举个例子:

    my $line = "   \"  detector located at x,y,z =\",  1p3e12.5,  abc";

分割后得到如下3个字段(注意分割后的字段前后没空格):
"  detector located at x,y,z ="
1p3e12.5
abc

情况2:  my $line = "   \"  detector located at x,y,z =\",  e12.5/\"  estimated, data \",  abc";

分割后得到如下3个字段:
"  detector located at x,y,z ="
e12.5/"  estimated, data "
abc

有啥简单的方法没?(希望我说清楚里)   如果能用一个split就好了
作者: yizhengming    时间: 2012-11-19 20:45
自己先顶一个  ....
作者: jason680    时间: 2012-11-19 21:06
本帖最后由 jason680 于 2012-11-19 21:11 编辑

回复 1# yizhengming


split(/([^",]*(?:"[^"]*")?[^",]*),/,$line);

perldoc -f split
perdoc  perlretut
perdoc  perlre

作者: kk861123    时间: 2012-11-19 21:49
使用Text::CSV_XS
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;
  5. use Text::CSV_XS;

  6. my $line = qq|   \"  detector located at x,y,z =\",  1p3e12.5,  abc|;
  7. my $csv = Text::CSV_XS->new( {
  8.     binary => 1,
  9.     allow_whitespace => 1,
  10.     allow_loose_quotes => 1,
  11. } );
  12. $csv->parse($line) or die $!;
  13. my @fields = $csv->fields();
  14. print Dumper \@fields;
复制代码

作者: yizhengming    时间: 2012-11-19 21:54
回复 3# jason680


    thinks  偷偷的学习split 去
作者: yizhengming    时间: 2012-11-19 21:55
回复 4# kk861123


    thinks....
作者: mcshell    时间: 2012-11-19 22:22
回复 1# yizhengming


    try
  1. split/,(?!.*",)|(?<="),/,$str;
复制代码

作者: yizhengming    时间: 2012-11-19 22:36
回复 7# mcshell

good  but:
    my $line = "abc, \"def\", ghi";
作者: mcshell    时间: 2012-11-19 22:46
回复 8# yizhengming


    perl不支持不定长否定逆序环视
   我觉得还是眼镜哥说的那个模块好用。。。我写的只是娱乐下拉
作者: yizhengming    时间: 2012-11-19 23:01
受 jason680 启发,  my @arr = $line =~ /("[^"]+"|[^,"\s]+(?:"[^"]+")*)/g;  这个也不错
作者: mcshell    时间: 2012-11-19 23:12
本帖最后由 mcshell 于 2012-11-19 23:23 编辑

回复 10# yizhengming


    学习了
  1. split/,(?!.*",)|(?<="),|,\s*(?=.")/,$str;
复制代码

作者: yizhengming    时间: 2012-11-19 23:32
回复 11# mcshell

so ..
my $line = "\"abc,efg\",  \"hig, ff, gg\"";
   
作者: iLRainyday    时间: 2012-11-20 08:36
mcshell 发表于 2012-11-19 22:46
回复 8# yizhengming


用\k可以模拟
作者: mcshell    时间: 2012-11-20 15:20
iLRainyday 发表于 2012-11-20 08:36
用\k可以模拟

看了一下午的\K
不知道还有哪种情况没有想到
  1. #!/usr/bin/perl
  2. use strict;
  3. while(<DATA>){
  4.         my @line = split/"[^"]+,?"\K,|^[^"]*\K,|,(?!.*",?)/;
  5.            print "$_\n" for @line;
  6. }
  7. __DATA__
  8. \"abc,efg\",  \"hig, ff, gg\"
  9. \"  detector located at x,y,z =\",  1p3e12.5,  abc, sdkfj
  10.   \"  detector located at x,y,z =\",  e12.5/\"  estimated, data \",  abc
  11. abc, \"def\", ghi
复制代码

作者: yizhengming    时间: 2012-11-20 15:32
回复 14# mcshell

my $line = "\"abc,efg\", d54.2,  e34.5\"hig, gg\"";   
   
作者: mcshell    时间: 2012-11-20 15:36
回复 15# yizhengming


    你在折磨我
作者: iLRainyday    时间: 2012-11-20 18:05
本帖最后由 iLRainyday 于 2012-11-20 18:07 编辑

自己写regex处理CSV?算了吧,effective perl programming里面的Item 115的标题就是:Don’t use regular expressions for comma-separated values.

Almost every single regular expression we’ve seen to handle CSV has failed
in some regard. Instead of wasting your time refining your regular expression
to handle more and more absurdities with this format, let the
Text::CSV_XS module handle it for you. It’s going to be faster than anything
you write anyway, and it’s certainly easier。

作者: yizhengming    时间: 2012-11-20 20:15
回复 17# iLRainyday


    哥 你猜错了, 让你见笑,小弟还真不知道这专业术语CSV,可能是我处理的文本串和CSV有点相似吧,回头要多做做功课里。另外建议 有这个时间背effective perl programming  不如向jason680 学习    多背背perl文档  
作者: iLRainyday    时间: 2012-11-20 21:31
本帖最后由 iLRainyday 于 2012-11-20 21:46 编辑

回复 18# yizhengming

谢谢建议。我买过一本perl language reference manual(perldoc的hard copy),背诵不敢说,看的也算是比较熟悉了。还有别的建议吗?

引用effective perl programming的原话是希望自己提的建议是有理有据,至少不是空口说的。你要是觉得我是在背书,你就无视好了。

PS. 放着前人总结好的经验不去学习,非要自己再从perldoc中总结,何况十之八九还总结不出来。我还真是没这个时间。

   
作者: zhlong8    时间: 2012-11-20 21:45
iLRainyday 发表于 2012-11-20 21:31
回复 18# yizhengming

谢谢建议。我买过一本perl language reference manual(perldoc的hard copy),背 ...


竟然有这书
作者: iLRainyday    时间: 2012-11-20 21:48
本帖最后由 iLRainyday 于 2012-11-20 21:50 编辑
zhlong8 发表于 2012-11-20 21:45
竟然有这书


嘿嘿,还真有。amazon.cn上就有,大概¥258吧。这个出版社还出过一本GCC的入门书,写的也挺好。


作者: yizhengming    时间: 2012-11-20 22:24
回复 19# iLRainyday

理解你的表白   说多背背perl文档 你理解不来的 (也没你想的那意思)   
   
作者: iLRainyday    时间: 2012-11-21 10:20
回复 22# yizhengming
行了,那就留着你词不达意的言外之意跟同道交流吧。


   
作者: yizhengming    时间: 2012-11-21 11:18
提示: 该帖被管理员或版主屏蔽
作者: yizhengming    时间: 2012-11-21 11:42
回复 23# iLRainyday


    好吧 既然你理解不了 那哥告诉你 要么就向(某某人)真心帮别人解决问题,要么别他妈的以这种自以为是,装逼的辞掉回些没觉悟的帖,
    还有你之前要的建议 哥通通给你 你敢要 哥就敢给  建议是 以后出门 别他妈的忘了给自己衣服上贴上牛逼的标签, 不然别人会识别不出来
    还有 进门之前先别喊, 先看是不是自己人      
背诵不敢说,看的也算是比较熟悉了
看完又能咋啦 能提出建设性思路吗
     给你这吊丝一句忠告做人低调 做事高调  哥以后不会回帖和你个丢人的东西纠结了
作者: mcshell    时间: 2012-11-21 23:59
回复 25# yizhengming
  1. my @line = split/^[^"]+?\K,|"[^"]+"\K,|,(?!.*")|\s+[^"]+?\K,/;
复制代码

LZ还有什么情况出现。。继续贴出来,这题让我对环视有了进一步的了解:wink:




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2