regex - Perl 使用 file2 从 file1 中删除单词

标签 regex linux perl

我正在使用 perl 脚本删除文本中的所有停用词。停用词逐行存储。我正在使用 Mac OSX 命令行并且 perl 已正确安装。

此脚本运行不正常,存在边界问题。

#!/usr/bin/env perl -w
# usage: script.pl words text >newfile
use English;

# poor man's argument handler
open(WORDS, shift @ARGV) || die "failed to open words file: $!";
open(REPLACE, shift @ARGV) || die "failed to open replacement file: $!";

my @words;
# get all words into an array
while ($_=<WORDS>) { 
  chop; # strip eol
  push @words, split; # break up words on line
}

# (optional)
# sort by length (makes sure smaller words don't trump bigger ones); ie, "then" vs "the"
@words=sort { length($b) <=> length($a) } @words;

# slurp text file into one variable.
undef $RS;
$text = <REPLACE>;

# now for each word, do a global search-and-replace; make sure only words are replaced; remove possible following space.
foreach $word (@words) { 
     $text =~ s/\b\Q$word\E\s?//sg;
}

# output "fixed" text
print $text;

样本.txt

$ cat sample.txt
how about i decide to look at it afterwards what
across do you think is it a good idea to go out and about i 
think id rather go up and above

停用词.txt

I
a
about
an
are
as
at
be
by
com
for
from
how
in
is
it
..

输出:

$ ./remove.pl stopwords.txt sample.txt 
i decide look fterwards cross do you think good idea go out d i 
think id rather go up d bove

如您所见,它使用 as fterwards 替换 afterwards。认为这是一个正则表达式问题。请有人可以帮我快速修补这个问题吗?感谢所有帮助:J

最佳答案

$word 的两边使用单词边界。目前,您只是在开始时检查它。

您不需要 \s? 条件和 \b 就位:

$text =~ s/\b\Q$word\E\b//sg;

关于regex - Perl 使用 file2 从 file1 中删除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084749/

相关文章:

php - 如何使用 PHP 验证正则表达式

c# - IE 用户代理正则表达式(包括 IE11 和紧凑 View )

c++ - 程序应该显示文件的最后 5 行,但不适用于大文件

linux - Makefile.PL - 没有这样的文件或目录

regex - 删除所有非键盘字符的正则表达式

linux - 对于文件中的每一行同步执行命令并保存到另一个文件的换行符

javascript - Jquery 将 css 类添加到父 <ul> 元素

regex - 反向正则表达式搜索

linux - 查找文件之间的公共(public)列位置

c - 暂停一个linux内核函数