I want to get the index as well as the results of a scan
"abab".scan(/a/)
I would like to have not only
=> ["a", "a"]
but also the index of those matches
[1, 3]
any suggestion?
I want to get the index as well as the results of a scan
"abab".scan(/a/)
I would like to have not only
=> ["a", "a"]
but also the index of those matches
[1, 3]
any suggestion?
Try this:
res = []
"abab".scan(/a/) do |c|
res << [c, $~.offset(0)[0]]
end
res.inspect # => [["a", 0], ["a", 2]]
It surprised me that there isn't any method similar to String#scan
which would return array of MatchData
objects, similar to String#match
. So, if you like monkey-patching, you can combine this with Todd's solution (Enumerator
is introduced in 1.9):
class Regexp
def scan str
Enumerator.new do |y|
str.scan(self) do
y << Regexp.last_match
end
end
end
end
#=> nil
/a/.scan('abab').map{|m| m.offset(0)[0]}
#=> [0, 2]