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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/Perl/172312.html

猜你在找的Perl相关文章