如果一个单词在一个文件中重复两次,如何使用ruby进行检查



我有一个大文件,我希望能够检查一个单词是否存在两次。

puts "Enter a word: "
$word = gets.chomp
if File.read('worldcountry.txt') # do something if the word entered is present twice...

如何检查文件worldcountry.txt是否包含我输入的$word的两倍?

我从中找到了我需要的东西:通过计数频率

在Gerry的帖子上用这个代码

word_count = 0
my_word = "input"
File.open("texte.txt", "r") do |f|
f.each_line do |line|
line.split(' ').each do |word|
word_count += 1 if word == my_word
end
end
end
puts "n" + word_count.to_s

谢谢,下次我会更加注意的。

如果文件不是太大,可以将其放入字符串中。假设:

str = File.read('cat')
#=> "There was a dog 'Henry' whonwas pals with a dog 'Buck' andna dog 'Sal'."
puts str
There was a dog 'Henry' who
was pals with a dog 'Buck' and
a dog 'Sal'.

假设给定的单词是'dog'

确认文件包含至少给定单词的两个实例

可以尝试匹配正则表达式

r1 = /bdogb.*bdogb/m
str.match?(r1)
#=> true

演示

确认文件中正好包含给定单词的两个实例

使用正则表达式来确定文件是否正好包含给定单词的两个实例有些复杂。让

r2 = /A(?:(?:.(?!bdogb))*bdogb){2}(?!.*bdogb)/m
str.match?(r1)
#=> false

演示


这两个正则表达式可以在自由间距模式下编写,使它们能够自我文档化。

r1 = /
bdogb       # match 'dog' surrounded by word breaks  
.*            # match zero or more characters
bdogb       # match 'dog' surrounded by word breaks
/m            # cause . to match newlines
r2 = /
A            # match beginning of string
(?:           # begin non-capture group
(?:         # begin non-capture group
.         # match one character
(?!       # begin negative lookahead
bdogb # match 'dog' surrounded by word breaks
)         # end negative lookahead
)           # end non-capture group
*           # execute preceding non-capture group zero or more times
bdogb     # match 'dog' surrounded by word breaks
)             # end non-capture group
{2}           # execute preceding non-capture group twice
(?!           # begin negative lookahead
.*          # match zero or more characters
bdogb     # match 'dog' surrounded by word breaks
)             # end negative lookahead
/xm           # # cause . to match newlines and invoke free-spacing mode

最新更新