Project

General

Profile

Actions

Feature #5606

closed

String#each_match(regexp)

Added by tomoakin (Tomoaki Nishiyama) over 12 years ago. Updated about 6 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

Updated by naruse (Yui NARUSE) over 12 years ago

String#scan ではダメな理由を、ユースケースを添えて示す必要があると思います。

Updated by tomoakin (Tomoaki Nishiyama) over 12 years ago

長い配列中のモチーフの存在位置のリストを作りたいので、位置(offset)が必要です。

String#scan ではダメかというと

longstring.scan(regex) do |matchstr|
m=Regexp.last_match
...
puts "#{m.begin(0)}-#{m.end(0)-1} some other info"
end

でとれば確かにとれるらしいけど、いかにもトリッキーな気がしますので、直接的に

longstring.each_match(regex) do |m|
...
puts "#{m.begin(0)}-#{m.end(0)-1} some other info"
end

と書ける方がうれしいです。

Updated by mame (Yusuke Endoh) almost 12 years ago

  • Status changed from Open to Feedback

遠藤です。

2011年11月10日17:19 Tomoaki Nishiyama :

String#scan ではダメかというと

longstring.scan(regex) do |matchstr|
m=Regexp.last_match
...
puts "#{m.begin(0)}-#{m.end(0)-1} some other info"
end

でとれば確かにとれるらしいけど、いかにもトリッキーな気がしますので、

$~ を使うのが普通です。

longstring.scan(regex) do
...
puts "#{$~.begin(0)}-#{$~.end(0)-1} some other info"
end

「いかにもトリッキー」というほどトリッキーとは思いませんが、
気持ちはわかります。しかし、String#scan と String#gsub の
ブロックパラメータをそろえろ! (#546) という話に関係しなく
もなく、話が進みにくいタイプの提案だと思いました。
機会を見つけてまつもとさんに直談判することをお勧めします。

いずれにせよ、ユースケースを示してください。

ところで、参照実装で

offset = m.end(0)

でなく

offset = m.begin(0)+1

になってるのは意図的でしょうか。

--
Yusuke Endoh

Updated by mame (Yusuke Endoh) over 11 years ago

  • Target version set to 2.6
Actions #5

Updated by naruse (Yui NARUSE) about 6 years ago

  • Target version deleted (2.6)
Actions #6

Updated by shyouhei (Shyouhei Urabe) over 5 years ago

  • Related to Feature #12745: String#(g)sub(!) should pass a MatchData to the block, not a String added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0