re = /(?(DEFINE)
(?<int>
(?:
\G|
(?!\n)
)
\s*\b\d{1,5}\s*
)
(?<timepart>
(
s|
m|
h|
d
)
\b
)
)
^
((?&int))
((?&timepart))
(?<string>.+)
$/xim
str = '2min baz
12 mins foo
1 hour 20 mins remind this
1sec
5 hours
1 sec foo
3 minutes 4 seconds
3333 sec do
1 hrs 20 mins 5 seconds remind this
2 days 3 hrs 20 mins 5 seconds remind this
foo 2sec bar //fail
2 s hi
3 m foo
3m bar
2d baz
9 h foobar'
# 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