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.
match_all
based on that. – Margarethematch_all
method should exist, assuming the requirement to be rather common. I must apologise in using the term "inelegant" for the quoted answer; I was just looking for a rather direct method instead. – Idden