It will break (throw an exception) if the string is not found in the
file because it does
not check whether m is non nil.
File.read() is a much simpler way to read the whole file at once than
File.open(…){|f| f.read}.
It is inefficient because it reads in the whole file just to extract
a portion of the file.
Even worse, it will break for large files.
It does more IO than necessary because it always reads the whole
file.
Logic is not encapsulated in a method which hinders reuse.
Compare that to my solution:
def r(f)
File.foreach f do |line|
match = line[/^CRP+TYH+(\d+)/, 1] and return match
end
nil
end
The file is processed line by line (but still IO uses buffering
underneath for efficiency). Reading is stopped as soon as the data is
found. It won’t break for large files.
We can even generalize the approach
def find_first_in_file(file, regexp, group = 1)
File.foreach file do |line|
match = regexp.match(line) and return match[group] || match[0]
end
nil
end
This will return the first match and if the regexp has a capturing group
it will return the capture of the first group. We would use it like
this
data = find_first_file(“myfile.txt”, /^CRP+TYH+(\d+)/)
If I understand you correctly, you want to read the second and third
entry of the first line beginning with ‘STS’. You can do this by using
capture groups in the regular expression (like Robert and Eric already
did in the posts above).
For example:
File.foreach ‘F:/testing.txt’ |line|
if line =~ /\ASTS+(\w+)+(\w+)/
puts $1, $2
break
end
end
When the regular expression is applied, the matched content of the
parenthesized subexpressions are saved in the variables $1 und $2. You
can then output these variables.
However, you should write a general method like Robert suggested. Then
you won’t have to think about new code every time you want to read a
certain line but simply use the method.