我想弄清楚如何使用Ruby从非结构化文本中提取日期。使用Ruby从文本解析日期
例如,我想解析这个字符串的日期“2010年2月1日中午12:00(东部标准时间)之后开始的应用程序将不被考虑。”
有什么建议吗?
我想弄清楚如何使用Ruby从非结构化文本中提取日期。使用Ruby从文本解析日期
例如,我想解析这个字符串的日期“2010年2月1日中午12:00(东部标准时间)之后开始的应用程序将不被考虑。”
有什么建议吗?
假设你只是想日期,而不是日期时间:
require 'date'
string = "Applications started after 12:00 A.M. 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
尝试慢性(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=>[]}]
将这个字符串总是在格式?日期字符串是否总是采用这种格式? –
该字符串不会。日期字符串将采用该格式。 – user274862