我有一个数据库,其中包含许多包含逗号分隔值的字段.我需要在Perl中拆分这些字段,这很简单,除了一些值后面跟着包含在括号中的嵌套CSV,我不想拆分.
例:
recycling,environmental science,interdisciplinary (e.g.,consumerism,waste management,chemistry,toxicology,government policy,and ethics),consumer education
拆分“,”给了我:
recycling environmental science interdisciplinary (e.g. consumerism waste management chemistry toxicology government policy and ethics) consumer education
我想要的是:
recycling environmental science interdisciplinary (e.g.,and ethics) consumer education
任何Perl正则表达式(perts)能伸出援手吗?
我试过修改我在类似的SO post中找到的正则表达式字符串,它没有返回任何结果:
#!/usr/bin/perl use strict; use warnings; my $s = q{recycling,consumer education}; my @parts = $s =~ m{\A(\w+) ([0-9]) (\([^\(]+\)) (\w+) ([0-9]) ([0-9]{2})}; use Data::Dumper; print Dumper \@parts;
解决方法
试试这个:
my $s = q{recycling,consumer education}; my @parts = split /(?![^(]+\)),/,$s;