如何在Ruby中匹配完整的单词而不是子串

这是我的代码

stopwordlist = "a|an|all" File.open('0_9.txt').each do |line| line.downcase! line.gsub!( /\b#{stopwordlist}\b/,'') File.open('0_9_2.txt', 'w') { |f| f.write(line) } end 

我想删除单词 – a,an和all但是,它也匹配子串并删除它们

输入示例 –

 Bromwell High is a cartoon comedy. It ran at the same time as some other programs about school life 

我得到输出 –

 bromwell high is cartoon comedy. it rt the same time s some other programs bout school life 

如您所见,它与子字符串匹配。

如何使它与单词匹配而不是子串?

| 正则表达式中的运算符占用最广泛的范围。 您的原始正则表达式匹配\baanall\b

将整个正则表达式更改为:

 /\b(?:#{stopwordlist})\b/ 

或将stopwordlist更改为正则表达式而不是字符串。

 stopwordlist = /a|an|all/ 

更好的是,您可能想要使用Regexp.union

 \ba\b|\ban\b|\ball\b 

试试这个。这会寻找单词边界。