String#each_match, kind of like String#scan but returning MatchData instead of the simple match.
It takes a block as well!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def each_match(regex) | |
result = [] | |
position = 0 | |
while match_data = regex.match(self, position) do | |
result << if block_given? then yield(match_data) else match_data end | |
position = match_data.end(match_data.size - 1) | |
end | |
result | |
end | |
end |
Author
eljojo
commented
Sep 1, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment