Project

General

Profile

Actions

Feature #5606

closed

String#each_match(regexp)

Added by tomoakin (Tomoaki Nishiyama) about 13 years ago. Updated almost 7 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-dev:44850]

Description

文字列上の正規表現に一致する場所の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もですが、、


Related issues 3 (2 open1 closed)

Related to Ruby master - Feature #5749: new method String#match_all neededAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDataAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #12745: String#(g)sub(!) should pass a MatchData to the block, not a StringFeedbackmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0