使用Ruby从文本解析日期

我正在试图弄清楚如何使用Ruby从非结构化文本中提取日期。

例如,我想解析这个字符串的日期“2010年2月1日午夜(美国东部时间)午夜12点之后开始的申请将不予考虑。”

有什么建议?

假设您只想要日期而不是日期时间:

require 'date' string = "Applications started after 12:00 AM Midnight (EST) February 1, 2010 will not be considered." r = /(January|February|March|April|May|June|July|August|September|October|November|December) (\d+{1,2}), (\d{4})/ if string[r] date =Date.parse(string[r]) puts date end 

尝试Chronic( http://chronic.rubyforge.org/ )它可能能够解析,否则你将不得不使用Date.strptime。

您也可以尝试一个可以帮助在字符串中查找日期的gem 。

Exapmle:

 input = 'circa 1960 and full date 07 Jun 1941' dates_from_string = DatesFromString.new dates_from_string.get_structure(input) #=> return # [{:type=>:year, :value=>"1960", :distance=>4, :key_words=>[]}, # {:type=>:day, :value=>"07", :distance=>1, :key_words=>[]}, # {:type=>:month, :value=>"06", :distance=>1, :key_words=>[]}, # {:type=>:year, :value=>"1941", :distance=>0, :key_words=>[]}]