How can I parse this text using regex (ruby)?
http://services.tvrage.com/tools/quickinfo.php?show=Chuck
I'm trying to parse that info, for example, get the Airtime,
Ai开发者_JAVA技巧rtime@Monday at 08:00 pm
I want to get what's after "Airtime@" till the end of the line, to just come out with "Monday at 08:00 pm". How can I do this?
Is there any reason you don't use the XML feeds?
require 'open-uri'
require 'nokogiri'
d = Nokogiri.XML(open 'http://services.tvrage.com/feeds/showinfo.php?sid=15614')
name = d.search('//showname').text # => 'Chuck'
day = d.search('//airday').text # => 'Monday'
time = d.search('//airtime').text # => '20:00'
net = d.search('//network[@country="US"]').text # => 'NBC'
puts "#{name} airs #{day}s at #{time} on #{net}."
# Chuck airs Mondays at 20:00 on NBC.
result = allyourtextdata[/Airtime@(.+)/,1]
Or if you are also going to use some another strings from this report:
hash = Hash[allyourtextdata.scan(/(.+?)@(.+)/)]
p hash["Airtime"] # this will print "Monday at 08:00 pm"
require 'net/http'
url=URI.parse('http://services.tvrage.com/tools/quickinfo.php?show=Chuck')
response = Net::HTTP.get_response(url)
data=response.body
puts data.scan(/.*Airtime@(.*)\n/)
Why regex? Any problem getting substring?
s = "Airtime@Monday at 08:00 pm"
puts s[8..-1] # => Monday at 08:00 pm
edit
ok, this is for other options
puts s[s.index('@') + 1..-1]
This is a great site for testing ruby regex expressions: http://rubular.com/
精彩评论