我有这样的数组
my @stopWords = ("and","this",....)
我的文字在此变量中
my $wholeText = "....and so this is...."
我想匹配标量WholeText中stopWords数组的每个元素的所有出现,并用空格替换它。
一种方法如下:
foreach my $stopW (@stopWords)
{
$wholeText =~ s/$stopW/ /;
}
这可以工作并替换所有停用词的每次出现。我只是想知道,是否有更短的方法。
像这样:
$wholeText =~ s/@stopWords/ /;
上面的方法似乎不起作用。
最佳答案
grep{$wholeText =~ s/\b$_\b/ /g}@stopWords;
关于perl - Perl替换运算符可以匹配数组中的元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4030363/