From: Joey Zhou Date: 2011-12-12T18:42:57+09:00 Subject: [ruby-core:41605] [ruby-trunk - Feature #5749] new method String#match_all needed Issue #5749 has been updated by Joey Zhou. Yui NARUSE wrote: > Why don't you use $~, $&, $`, $', $+, $1, $2, .. in scan' block parameter? You reminds me! Yes, what I want can be done in this tricky way. Thank you! However, I think relying on these special global variables is just an expedient. If there is an explicit method, it's much more user-friendly and readable. When I wanted the function, what I did is to consult the API, attempting to find a proper method, not thinking how to play with those magic punctuation. Maybe most people is just like me... ---------------------------------------- Feature #5749: new method String#match_all needed http://redmine.ruby-lang.org/issues/5749 Author: Joey Zhou Status: Open Priority: Normal Assignee: Category: Target version: The String class should contain an instance method 'match_all', which is a mixture of 'match' and 'scan'. The method 'scan' is not a very powerful tool, its result(the yielding thing) is just a matched string or an array of captured strings. p 'a1bc2de3f'.scan(/(.)\d(.)/) # [["a", "b"], ["c", "d"], ["e", "f"]] If the regex argument contains groups, I even cannot get the whole matched string, and no information about the matched offsets. So, a 'match_all' is very necessary. It scan the string, finding every matched, and yielding *MatchData instance* to the following block. Here's a simple implemention in Ruby: class String def match_all(re,i=0) if block_given? while m = self.match(re,i) yield m i = m.end(0) end return self else ary = [] while m = self.match(re,i) ary << m i = m.end(0) end return ary end end end However, it is not efficient in the 'while m = self.match(re,i)' way, because it scan the string again and again. If string is UTF8-encoded and contains out-of-ASCII characters, I'm afraid getting the start index of it is so expensive. So, I think a built-in 'match_all' method, which behaves just like 'scan' but yield MatchData, is needed. Please consider it, thank you! -- http://redmine.ruby-lang.org