假设:
my $s1='A "sentence" ... is a concatination of nondelimiters followed by delimiters,
followed by nondelimiters, followed by delimiters, ...';
或
my $s2='"A "sentence" ... is a concatination of delimiters followed by nondelimiters,
followed by delimiters, followed by nondelimiters, ..."';
我尝试使用标量$ d中的分隔符设置将regexp拆分为列表@a:
my $d = ' ,."'; # delimiter set
my @a = $s1 =~ /([^$d]+)([$d]+)/g; # gives |A| "|sentence|" ... |is| |a| |...
在第二种情况下,句子可以使用
my @a = $s2 =~ /([$d]+)([^$d]+)/g; # gives |"|A| "|sentence|" ... |is| |a|...
如何使用单个regexp处理这两种情况?需要帮助 - 请。
答案 0 :(得分:1)
您需要连续的分隔符或非分隔符块。只需使用
$s =~ /([$d]+|[^$d]+)/g;