我有一个文本,我想在其中找到所有匹配项
(例如,匹配 /d.g/
的每个模式)
我需要将这些模式放在列表中并从原始文本中删除。
操作:dog and dig where dug in dug
应该给我:
(狗,挖,挖,挖)。
文本应更改为:and where in
我可以通过传递文本两次来做到这一点,但这会是双重工作吗?
最佳答案
这是另一种选择:
use strict;
use warnings;
my $str = 'dog and dig where dug in dug';
my @matches;
$str =~ s/\b(d.g)\b/push @matches, $1; ''/ge;
print $str, "\n";
print join ', ', @matches;
输出:
and where in
dog, dig, dug, dug
关于regex - Perl regexp - 检索要列出的所有匹配项并将其从文本中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13980677/