perl - Perl替换运算符可以匹配数组中的元素吗?

标签 perl

我有这样的数组

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/

相关文章:

git - 在 Git::Repository 中使用密码

mysql - Perl DBI DBD::mysql 从 mysql 服务器获取数据库名称

perl - 如何将导入语义实现到当前 block 范围?

mysql - Perl 和 MySql 中的 utf8 编码

perl - 跳出子程序

perl - 在 Perl 中查找数组的大小

Perl 内置退出并在一个命令中打印

perl - 如何重新定义 Perl 类方法?

php rand vs Perl rand

perl - 如何让 "perl Makefile.PL"使用所有需要的路径?