ruby - Getting a string from a file -
ruby - Getting a string from a file -
this question exact duplicate of:
what each part of code mean? ruby [closed]i have been using:
open("some.txt") { |f| f.each_line.find_all{ |line| /re/.match(line) } }
what each component of piece of code mean?
one way map results operation want them. if wanted first 3 words do:
open("some.txt") { |f| f.each_line.find_all{ |line| /re/.match(line)}.map{|line| line.split[0...3].join(' ')}
update: above assumes recipe first word on line. instead of doing that, can modify regex takes next word characters until space \w*\s
, repeats e.g. 2 or 3 times {2, 3}
:
open("some.txt") { |f| f.each_line.find_all{ |line| puts /re(\w*\s){2,3}/.match(line) } }
(play around regex needed on http://rubular.com/)
ruby file screen-scraping
Comments
Post a Comment