这应该能满足您的需求。它的工作方式与
Text::CSV_PP
中的代码非常相似,但不允许在字段内使用转义字符,因为您说您没有这样的字符。
use strict;
use warnings;
use 5.010;
my $re = qr/(?| "\( ( [^()""]* ) \)" | \( ( [^()]* ) \) | " ( [^"]* ) " | ( [^,]* ) ) , \s* /x;
my $line = '(Date, Notional 1), "Date, Notional 2", "(Date, Notional 3)"';
my @fields = "$line," =~ /$re/g;
say "<$_>" for @fields;
输出
<Date, Notional 1>
<Date, Notional 2>
<Date, Notional 3>
更新
这里提供适用于旧版本Perl(10版之前)的版本,它没有正则表达式分支重置结构。它产生与上述相同的输出。
use strict;
use warnings;
use 5.010;
my $re = qr/(?: "\( ( [^()""]* ) \)" | \( ( [^()]* ) \) | " ( [^"]* ) " | ( [^,]* ) ) , \s* /x;
my $line = '(Date, Notional 1), "Date, Notional 2", "(Date, Notional 3)"';
my @fields = grep defined, "$line," =~ /$re/g;
say "<$_>" for @fields;