微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

regex – Perl在逗号上拆分列表除了在括号内?

我有一个数据库,其中包含许多包含逗号分隔值的字段.我需要在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;

原文地址:https://www.jb51.cc/Perl/172312.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐