1

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

例如,我想从这个字符串中解析日期“将不考虑 2010 年 2 月 1 日午夜 (EST) 午夜 12:00 之后开始的应用程序”。

有什么建议么?

4

3 回答 3

7

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

于 2010-02-17T00:42:38.230 回答
0

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

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
于 2010-02-17T03:02:51.223 回答
0

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

示例:

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=>[]}]
于 2015-06-01T11:53:27.860 回答