gpt4 book ai didi

regex - Perl 在逗号上拆分列表,除非在括号内?

转载 作者:行者123 更新时间:2023-12-04 06:24:31 26 4
gpt4 key购买 nike

我有一个数据库,其中包含多个包含逗号分隔值的字段。我需要在 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., consumerism, waste management, chemistry, toxicology, government policy, and ethics)
consumer education

任何 Perl 正则表达式(perts)都可以帮忙吗?

我尝试修改我在类似 SO post 中找到的正则表达式字符串不返回任何结果:
#!/usr/bin/perl

use strict;
use warnings;

my $s = q{recycling, environmental science, interdisciplinary (e.g., consumerism, waste management, chemistry, toxicology, government policy, and ethics), 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, environmental science, interdisciplinary (e.g., consumerism, waste management, chemistry, toxicology, government policy, and ethics), consumer education};

my @parts = split /(?![^(]+\)), /, $s;

关于regex - Perl 在逗号上拆分列表,除非在括号内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9435564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com