Ruby regular expression matching enumerator with n

2019-06-03 18:15发布

问题:

Consider a string like this to extract the time information:

str = "Sun rises at 6:23 am & sets at 5:45 pm; Moon comes up by 7:20 pm and goes down by 3:45 am"

I wish to have an enumerator like scan but one that can get me the MatchData objects instead of arrays as available from scan.

For instance, I can write:

str.scan( /(?<time>\d:\d{2}) (?<meridiem>am|pm)/ ){ |arr| p arr }

to get:

["6:23", "am"] ["5:45", "pm"] ["7:20", "pm"] ["3:45", "am"]

But, I wonder if there something like this:

str.match_all( /(?<time>\d:\d{2}) (?<meridiem>am|pm)/ ){ |md| p md }

to get:

#<MatchData "6:23 am" time:"6:23" meridiem:"am"> #<MatchData "5:45 pm" time:"5:45" meridiem:"pm"> #<MatchData "7:20 pm" time:"7:20" meridiem:"pm"> #<MatchData "3:45 am" time:"3:45" meridiem:"am">

Saw an answer in a previous question, but I feel its an inelegant solution. So checking up in case things have changed over the last couple of years since the answer was posted.

回答1:

Very identical to the answer you have already seen, but slightly different.

str = "Sun rises at 6:23 am & sets at 5:45 pm; Moon comes up by 7:20 pm ..."
str.gsub(/(?<time>\d:\d{2}) (?<meridiem>am|pm)/).map{ Regexp.last_match } 

#=> [#<MatchData "6:23 am" time:"6:23" meridiem:"am">, #<MatchData "5:45 pm" ...