对于示例数据,例如:
my $temp_comment =
'CR1234: "Then some text"
CRs 2345, 3456, 45, 567
CR678 "Some Text"';
尝试:
$temp_comment =~ s/(,)|[^\d\n]+/$1?' ':''/semg;
或者,如果您想靠近字符串模板:
$temp_comment =~ s/ ^ # multi-line mode, line start
\s* # leading blanks?
CR # CR tag
\D* # non-number stuff
( # start capture group
(?:\d+ [,\s]*)+ # find (number, comma, space) groups
) # end capture group
\D* # skip remaining non-number stuff
$ # multi-line mode, line end
/$1/mxg; # set multi-line mode + regex comments "x"
但您必须在后续步骤中删除数字组中的逗号。
$temp_comment =~ tr/,//d; # remove commas in the whole string
或者
$temp_comment =~ s/(?<=\d),(?=\s\d)//g; # remove commas between numbers '11, 22'
对于“单步”,您必须使用/e
修饰符:
$temp_comment =~ s{ ^ # line start
\s* # leading blanks?
CR # CR tag
\D* # non-number stuff
((?:\d+ [,\s]*)+) # single or group of numbers
\D* # non number stuff
$ # line end
}
{do{(local$_=$1)=~y/,//d;$_}}mxeg;
根据上面的数据,这将导致:
1234
2345 3456 45 567
678
但实际上,如果可能, 请使用更简单的两步方法。后一种正则表达式可能是您的继任者的维护噩梦。