免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: cnmtchbgs
打印 上一主题 下一主题

如何忽略双引号中的分割符 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2011-01-04 16:01 |只看该作者
把 [        DISCUZ_CODE_0        ] 换成 $&

论坛徽章:
0
12 [报告]
发表于 2011-01-04 16:02 |只看该作者
perl -e '$_= "ab:123,bc:\"333,444\",cd:456"; while(/\w+"?.*?(?(1)"(?=,|$)/g){print $&,"\n";}'

论坛徽章:
0
13 [报告]
发表于 2011-01-04 16:03 |只看该作者
perl -e '$_= "ab:123,bc:\"333,444\",cd:456"; while(/\w+:(")?.*?(?(1)")(?=,|$)/g){print $&,"\n";}'

论坛徽章:
0
14 [报告]
发表于 2011-01-04 16:11 |只看该作者
回复 13# meihuaqi


    不知道你调试过没有,我拿你这个最新的代码试验了一下,同样的错误。很奇怪的,难道跟操作系统有关,我在win vista下面测试的

论坛徽章:
0
15 [报告]
发表于 2011-01-04 16:17 |只看该作者
回复 13# meihuaqi


    放到perl脚本文件里面执行,结果是正确的,我研究一下这两者的区别。

论坛徽章:
0
16 [报告]
发表于 2011-01-04 16:17 |只看该作者
无奈了
把这行代码写到文件中执行
$_= "ab:123,bc:\"333,444\",cd:456"; while(/\w+:(")?.*?(?(1)")(?=,|$)/g){print $&,"\n";}

论坛徽章:
0
17 [报告]
发表于 2011-01-04 16:19 |只看该作者
回复 16# meihuaqi


    放在文件中执行是没有问题的,这个就有意思了

论坛徽章:
0
18 [报告]
发表于 2011-01-04 16:24 |只看该作者
回复 13# meihuaqi


    请详细介绍一下/\w+"?.*?(?(1)"(?=,|$)/g  谢谢

论坛徽章:
0
19 [报告]
发表于 2011-01-04 16:38 |只看该作者
回复 16# meihuaqi


    同上,我也不明白这句话的具体含义,只知道是非贪婪匹配。

论坛徽章:
0
20 [报告]
发表于 2011-01-04 17:49 |只看该作者
貌似这个问题回答过很多相似的。。

perldoc -q "delimited string"
Found in /usr/lib/perl5/5.8.8/pod/perlfaq4.pod
       How can I split a [character] delimited string except when inside [character]?

       Several modules can handle this sort of pasing---Text::Balanced, Text::CSV, Text::CSV_XS, and
       Text:arseWords, among others.

       Take the example case of trying to split a string that is comma-separated into its different fields. You
       can't use "split(/,/)" because you shouldn't split if the comma is inside quotes.  For example, take a
       data line like this:

           SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Error, Core Dumped"

       Due to the restriction of the quotes, this is a fairly complex problem.  Thankfully, we have Jeffrey
       Friedl, author of Mastering Regular Expressions, to handle these for us.  He suggests (assuming your
       string is contained in $text):

            @new = ();
            push(@new, $+) while $text =~ m{
                "([^\"\\]*(?:\\.[^\"\\]*)*)",?  # groups the phrase inside the quotes
              | ([^,]+),?
              | ,
            }gx;
            push(@new, undef) if substr($text,-1,1) eq ',';

       If you want to represent quotation marks inside a quotation-mark-delimited field, escape them with back-
       slashes (eg, "like \"this\"".

       Alternatively, the Text:arseWords module (part of the standard Perl distribution) lets you say:

           use Text:arseWords;
           @new = quotewords(",", 0, $text);

       There's also a Text::CSV (Comma-Separated Values) module on CPAN.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP