https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112012-07-27T16:30:39ZRuby Issue Tracking SystemRuby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=284722012-07-27T16:30:39Zprijutme4ty (Ilya Vorontsov)prijutme4ty@gmail.com
<ul></ul><p>Simple implementation:</p>
<p>class String<br>
def each_match(pattern, &block)<br>
return Enumerator.new(self, :each_match, pattern) unless block_given?<br>
text = self<br>
m = text.match(pattern)<br>
while m<br>
yield m<br>
text = text[m.end(0)..-1]<br>
m = text.match(pattern)<br>
end<br>
end<br>
end</p> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=284802012-07-27T18:54:38ZEregon (Benoit Daloze)
<ul></ul><p>=begin<br>
You can use (({String#scan})) with the block form and (({$~})) (as well as other Regexp-related globals) for this:</p>
<pre><code>> text="x:1 y:12 ; x:33 y:2"
> text.scan(/x:(?<x>\d+) y:(?<y>\d+)/) { p [$~[:x],$~[:y]] }
["1", "12"]
["33", "2"]
</code></pre>
<p>Please check your Regexp and give an example of (({text})) next time.<br>
=end</p> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=285282012-07-29T20:13:09Zprijutme4ty (Ilya Vorontsov)prijutme4ty@gmail.com
<ul></ul><p>Thank you for a solution! I always forgot about regexp global vars. Though I suggest that using a special method here is more clear. So what'd you say about String#each_match and Regexp#each_match<br>
Yes, implementation is as simple as<br>
class String<br>
def each_match(pat)<br>
scan(pat){ yield $~ }<br>
end<br>
end</p>
<p>and similar for Regexp.</p>
<p>Eregon (Benoit Daloze) wrote:</p>
<blockquote>
<p>=begin<br>
You can use (({String#scan})) with the block form and (({$~})) (as well as other Regexp-related globals) for this:</p>
<pre><code>> text="x:1 y:12 ; x:33 y:2"
> text.scan(/x:(?<x>\d+) y:(?<y>\d+)/) { p [$~[:x],$~[:y]] }
["1", "12"]
["33", "2"]
</code></pre>
<p>Please check your Regexp and give an example of (({text})) next time.<br>
=end</p>
</blockquote> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=285322012-07-29T22:28:51Ztrans (Thomas Sawyer)
<ul></ul><p>+1 I have definitely used this before (as Facets' #mscan).</p> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=285362012-07-29T23:52:28ZEregon (Benoit Daloze)
<ul></ul><p>prijutme4ty (Ilya Vorontsov) wrote:</p>
<blockquote>
<p>Though I suggest that using a special method here is more clear.<br>
So what'd you say about String#each_match and Regexp#each_match</p>
</blockquote>
<p>I did indeed somewhat expected String#scan to yield a MatchData object, instead of $~.captures.<br>
I'm in favor of String#each_match, it might be a nice addition and the name is clear, but the naming is different from the usual regexp methods on String, and it might not be worth to add a method (I agree $~ is not the prettiest thing around).</p>
<p>I think Regexp#each_match does not convey well what it does though.</p> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=287122012-08-08T00:51:39Ztomoakin (Tomoaki Nishiyama)tomoakin@staff.kanazawa-u.ac.jp
<ul></ul><p>+1 to have a method to return MatchData.<br>
This is related to (or duplicate of) <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: new method String#match_all needed (Assigned)" href="https://redmine.ruby-lang.org/issues/5749">#5749</a> and <a class="issue tracker-2 status-7 priority-4 priority-default closed" title="Feature: String#each_match(regexp) (Feedback)" href="https://redmine.ruby-lang.org/issues/5606">#5606</a>.</p>
<p>Even with the simple implementation I think to establish a standard<br>
name and specification.</p> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=333382012-11-20T23:31:42Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li><li><strong>Target version</strong> set to <i>2.6</i></li></ul> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=688102017-12-25T18:15:06Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul> Ruby master - Feature #6802: String#scan should have equivalent yielding MatchDatahttps://redmine.ruby-lang.org/issues/6802?journal_id=748022018-11-08T07:31:03Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-7 priority-4 priority-default closed" href="/issues/12745">Feature #12745</a>: String#(g)sub(!) should pass a MatchData to the block, not a String</i> added</li></ul>