J
J
Jack-8882017-10-23 15:50:27
Ruby on Rails
Jack-888, 2017-10-23 15:50:27

How to extract the desired data from a string with a regular expression?

I have a line like "9:00 every 5 days"
I need to pull out separately:
1) time time = "9:00"
2) number of days days = "5"
I need two regular expressions, three hours of Google did not help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Bordenyuk, 2017-10-23
@HighQuality

str = '9:00 every 5 days'
/(\d+)\sdays?\z/.match(str)[1] # => "5"
/([^\s]+)\s/.match(str)[1] # => "9:00"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question