Forum: Ruby-core [ruby-trunk - Feature #6802][Open] String#scan should have equivalent yielding MatchData

Posted by prijutme4ty (Ilya Vorontsov) (Guest)
on 2012-07-27 09:18
(Received via mailing list)
Issue #6802 has been reported by prijutme4ty (Ilya Vorontsov).

----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by prijutme4ty (Ilya Vorontsov) (Guest)
on 2012-07-27 09:31
(Received via mailing list)
Issue #6802 has been updated by prijutme4ty (Ilya Vorontsov).


Simple implementation:

class String
  def each_match(pattern, &block)
    return Enumerator.new(self, :each_match, pattern)  unless 
block_given?
    text = self
    m = text.match(pattern)
    while m
      yield m
      text = text[m.end(0)..-1]
      m = text.match(pattern)
    end
  end
end
----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-28472

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by Eregon (Benoit Daloze) (Guest)
on 2012-07-27 11:55
(Received via mailing list)
Issue #6802 has been updated by Eregon (Benoit Daloze).


=begin
You can use (({String#scan})) with the block form and (({$~})) (as well 
as other Regexp-related globals) for this:

    > text="x:1 y:12 ; x:33 y:2"
    > text.scan(/x:(?<x>\d+) y:(?<y>\d+)/) { p [$~[:x],$~[:y]] }
    ["1", "12"]
    ["33", "2"]

Please check your Regexp and give an example of (({text})) next time.
=end

----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-28480

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by prijutme4ty (Ilya Vorontsov) (Guest)
on 2012-07-29 13:13
(Received via mailing list)
Issue #6802 has been updated by prijutme4ty (Ilya Vorontsov).


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
Yes, implementation is as simple as
class String
  def each_match(pat)
    scan(pat){ yield $~ }
  end
end

and similar for Regexp.


Eregon (Benoit Daloze) wrote:
> =begin
> You can use (({String#scan})) with the block form and (({$~})) (as well as other 
Regexp-related globals) for this:
>
>     > text="x:1 y:12 ; x:33 y:2"
>     > text.scan(/x:(?<x>\d+) y:(?<y>\d+)/) { p [$~[:x],$~[:y]] }
>     ["1", "12"]
>     ["33", "2"]
>
> Please check your Regexp and give an example of (({text})) next time.
> =end


----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-28528

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by Thomas Sawyer (7rans)
on 2012-07-29 15:38
(Received via mailing list)
Issue #6802 has been updated by trans (Thomas Sawyer).


+1 I have definitely used this before (as Facets' #mscan).
----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-28532

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by Eregon (Benoit Daloze) (Guest)
on 2012-07-29 16:55
(Received via mailing list)
Issue #6802 has been updated by Eregon (Benoit Daloze).


prijutme4ty (Ilya Vorontsov) wrote:
> 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

I did indeed somewhat expected String#scan to yield a MatchData object, 
instead of $~.captures.
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).

I think Regexp#each_match does not convey well what it does though.
----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-28536

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by tomoakin (Tomoaki Nishiyama) (Guest)
on 2012-08-07 17:52
(Received via mailing list)
Issue #6802 has been updated by tomoakin (Tomoaki Nishiyama).


+1 to have a method to return MatchData.
This is related to (or duplicate of) #5749 and #5606.

Even with the simple implementation I think to establish a standard
name and specification.
----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-28712

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by fahmisetiawand (Fahmi Setiawan) (Guest)
on 2012-10-03 11:27
(Received via mailing list)
Issue #6802 has been updated by fahmisetiawand (Fahmi Setiawan).


http://alkian.blogspot.com/2012/09/cara-menyembuhk...
http://alkian.blogspot.com/2012/09/keretamini-kere...
http://alkian.blogspot.com/2012/10/kata-kata-mutia...
http://alkian.blogspot.com/2012/08/personil-coboy-...
http://alkian.blogspot.com/2012/10/hasil-dan-klase...
----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-29971

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Posted by mame (Yusuke Endoh) (Guest)
on 2012-11-20 15:32
(Received via mailing list)
Issue #6802 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)
Target version set to next minor


----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-33338

Author: prijutme4ty (Ilya Vorontsov)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor


Ruby should have method to obtain not an array of arrays but of 
MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be 
#each_match
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.