re = /(?:(?P<Day1>\d{1,2})(?:\W*(?P<Month1>[A-Z][a-z]*\b))|(?:(?P<Month2>[A-Z][a-z]*\b)\W*)(?:(?P<Day2>\d{1,2})(?:\b|th|st|nd))?|(?P<Month3>\d{1,2})\W+(?P<Day3>\d{1,2})|(?P<Month4>\d{1,2}))?\W+?(?P<Year>\d{2,4}(?=$|;))/m
str = '04/20/2009
04/20/09
4/20/09
4/3/09
Mar-20-2009
Mar 20, 2009
March 20, 2009
Mar. 20, 2009
Mar 20 2009;
20 Mar 2009
20 March 2009
20 Mar. 2009
20 March, 2009
Mar 20th, 2009
Mar 21st, 2009
Mar 22nd, 2009
Feb 2009
Sep 2009
Oct 2010
6/2008
12/2009
2009
2010'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html