Ruby 如何检查字符串是否包含今日';以特定格式显示日期

Ruby 如何检查字符串是否包含今日';以特定格式显示日期,ruby,string,rss,Ruby,String,Rss,我正在解析一些RSS提要,这些提要汇总了给定城市中发生的事情。我只对今天发生的事情感兴趣 目前,我有以下几点: require 'rubygems' require 'rss/1.0' require 'rss/2.0' require 'open-uri' require 'shorturl' source = "http://rss.feed.com/example.xml" content = "" open(source) do |s| content = s.read end rss

我正在解析一些RSS提要,这些提要汇总了给定城市中发生的事情。我只对今天发生的事情感兴趣

目前,我有以下几点:

require 'rubygems'
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'shorturl'

source = "http://rss.feed.com/example.xml"
content = ""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)

t = Time.now
day = t.day.to_s
month = t.strftime("%b")

rss.items.each do |rss|
  if "#{rss.title}".include?(day)&&(month)
    # does stuff with it
  end
end
当然,通过检查标题(我知道它包含以下格式的事件日期:“(2011年4月2日)”)是否包含日期和月份(例如“2”和“5”),我还可以获得关于5月12日、5月20日等事件的信息。我怎样才能做到万无一失,只看今天的活动


下面是一个示例标题:“Diggin Deeper@The Big Chill House(5月12日11)”

使用类似以下内容:

def check_day(date)
  t = Time.now
  day = t.day.to_s
  month = t.strftime("%b")

  if date =~ /^#{day}nd\s#{month}\s11/
    puts "today!"
  else
    puts "not today!"
  end
end

check_day "3nd May 11" #=> today!
check_day "13nd May 11" #=> not today!
check_day "30nd May 11" #=> not today!

如果标题包含其他数字,则可能会出现问题。标题是否在日期周围有任何边界字符,例如日期前的连字符或括号?将这些添加到正则表达式中可以避免麻烦。你能给我们举个例子吗?(另一种方法是使用Time#strftime创建一个字符串,该字符串将与标题中显示的日期完全匹配,然后只使用字符串#include?与该字符串一起使用,但我认为没有一种优雅的方式将“th”/“nd”/“rd”等放在日期上。)

是的,日期周围有括号。使用正则表达式似乎是一个非常优雅的解决方案。我在问题中添加了示例标题以澄清问题。谢谢
today = Time.now.strftime("%d:%b:%y")
if date_string =~ /(\d*).. (.*?) (\d\d)/
  article_date = sprintf("%02i:%s:%s", $1.to_i, $2, $3)
  if today == article_date
    #this is today
  else
    #this is not today
  end
else
  raise("No date found in title.")
end