I have a string ruby something like this.
我有一个像这样的字符串ruby。
mystring = "some string value with a date 02/02/2002"
and I would like to extract just the date in order to store it in a database. I am pretty sure I'm going to need some sort of regex and way of searching for the pattern and grabbing it from the string.
我想提取日期,以便将其存储在数据库中。我很确定我需要某种正则表达式以及搜索模式并从字符串中获取它的方法。
Not real sure what needs to be done here.
不确定这里需要做什么。
I found this Regex online that is supposed to allow you to match that date formate.
我发现这个Regex在线可以让你匹配那个日期格式。
reg = Regexp.new("^\d{1,2}\/\d{1,2}\/\d{4}$")
How would I go about using that to parse out the above date from the string ?
我将如何使用它来解析字符串中的上述日期?
3 个解决方案
#1
How about this?
这个怎么样?
s = "some string value with a date 02/02/2002"regex = /\d{1,2}\/\d{1,2}\/\d{4}/s[regex] # => "02/02/2002"
Note that you don't need the ^
and $
in your regex as you won't be matching the string in its entirety.
请注意,您不需要正则表达式中的^和$,因为您不会完全匹配字符串。
#2
Kinda cool:
1.9.2-p290 :005 > "some string value with a date 02/02/2002".to_date=> Sat, 02 Feb 20021.9.2-p290 :013 > "some another string 05/07/2012 with stuff after".to_date=> Thu, 05 Jul 2012
And then do with it anything you want since its class is Date.
然后用它做任何你想要的东西,因为它的类是Date。
(I know it's not regex, but I still think it's cool ;P)
(我知道这不是正则表达式,但我仍然觉得它很酷; P)
#3
You can try https://rubygems.org/gems/dates_from_string:
您可以尝试https://rubygems.org/gems/dates_from_string:
text = "1988-1990 and 2000 and one more date 28.04.2015" dates_from_string = DatesFromString.new()dates_from_string.get_structure(text)#=> returns# [{:type=>:year, :value=>"1988", :distance=>0, :key_words=>[]},# {:type=>:year, :value=>"1990", :distance=>2, :key_words=>[]},# {:type=>:year, :value=>"2000", :distance=>5, :key_words=>[]},# {:type=>:year, :value=>"2015", :distance=>0, :key_words=>[]},# {:type=>:month, :value=>"04", :distance=>0, :key_words=>[]},# {:type=>:day, :value=>"28", :distance=>0, :key_words=>[]}]
#1
How about this?
这个怎么样?
s = "some string value with a date 02/02/2002"regex = /\d{1,2}\/\d{1,2}\/\d{4}/s[regex] # => "02/02/2002"
Note that you don't need the ^
and $
in your regex as you won't be matching the string in its entirety.
请注意,您不需要正则表达式中的^和$,因为您不会完全匹配字符串。
#2
Kinda cool:
1.9.2-p290 :005 > "some string value with a date 02/02/2002".to_date=> Sat, 02 Feb 20021.9.2-p290 :013 > "some another string 05/07/2012 with stuff after".to_date=> Thu, 05 Jul 2012
And then do with it anything you want since its class is Date.
然后用它做任何你想要的东西,因为它的类是Date。
(I know it's not regex, but I still think it's cool ;P)
(我知道这不是正则表达式,但我仍然觉得它很酷; P)
#3
You can try https://rubygems.org/gems/dates_from_string:
您可以尝试https://rubygems.org/gems/dates_from_string:
text = "1988-1990 and 2000 and one more date 28.04.2015" dates_from_string = DatesFromString.new()dates_from_string.get_structure(text)#=> returns# [{:type=>:year, :value=>"1988", :distance=>0, :key_words=>[]},# {:type=>:year, :value=>"1990", :distance=>2, :key_words=>[]},# {:type=>:year, :value=>"2000", :distance=>5, :key_words=>[]},# {:type=>:year, :value=>"2015", :distance=>0, :key_words=>[]},# {:type=>:month, :value=>"04", :distance=>0, :key_words=>[]},# {:type=>:day, :value=>"28", :distance=>0, :key_words=>[]}]