You are trying to read the file line by line. In such a case /m
multiline modifier makes no sense. You are to read the entire file and then parse it for whatever you want:
content = File.read('test.txt')
content.scan(/\.T(.*?)\.B/m) { |mtch|
puts mtch
}
UPD
To put the scan results to hash as in the example you need either flatten
method of an array:
content = File.read('test.txt')
# flatten the array ⇓⇓⇓⇓⇓⇓⇓
words = content.scan(/\.T(.*?)\.B/m).flatten
words.each …
or block within scan
method:
content = File.read('test.txt')
freqs = {}
content.scan(/\.T(.*?)\.B/m) { |mtch|
(freqs[mtch] ||= 0) += 1
}
…
UPD2 To split
the resulting array of sentenses to array of words:
arr = ["Preliminary Report International", "Fingers or Fists"]
arr.map {|e| e.split(' ')}.flatten.map(&:downcase)
# ⇒ ["preliminary", "report", "international", "fingers", "or", "fists"]
Here first map
iterates array elements and transforms them to arrays of splitten words, flatten
produces plain array from yielded array of arrays, and, finally, downcase
is here because you’ve requested the downcased words in your example.
Hope it helps.