From: "mame (Yusuke Endoh)" Date: 2012-11-20T21:55:19+09:00 Subject: [ruby-dev:46560] [ruby-trunk - Feature #5606] String#each_match(regexp) Issue #5606 has been updated by mame (Yusuke Endoh). Target version set to next minor ---------------------------------------- Feature #5606: String#each_match(regexp) https://bugs.ruby-lang.org/issues/5606#change-33258 Author: tomoakin (Tomoaki Nishiyama) Status: Feedback Priority: Low Assignee: Category: Target version: next minor 文字列上の正規表現に一致する場所のoffsetを順に処理できるような イテレータが欲しかったのですが、 ざっと検索すると1996年 http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/1206 の昔から話はあり、 http://stackoverflow.com/questions/6804557/how-do-i-get-the-match-data-for-all-occurrences-of-a-ruby-regular-expression-in 全くないという事はなくて、それなりに需要がありそうです。 class String def each_match(pattern, offset=0) while(m = self.match(pattern, offset)) offset = m.begin(0)+1 yield m end end end いかがしょうか? 対称にはRegexp#each_matchもですが、、 -- http://bugs.ruby-lang.org/