re = /^(?:(\d+)\sYears?)?,?\s?(?:and\s)?(?:(\d+)\sMonths?)?,?\s?(?:and\s)?(?:(\d+)\sDays?)?$/mi
str = '3 Years, 1 Month, and 60 Days
2 Months, and 1 Day
3 Days
1 Month
5 Years, and 2 Months
7 Years, and 1 Day
1 Year, 1 Month, and 1 Day
700'
# 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