From: Ammar Ali Date: 2010-11-05T19:21:23+09:00 Subject: [ruby-core:33062] Re: Suggestion for MatchData#first and #last On Fri, Nov 5, 2010 at 11:34 AM, Robert Klemme wrote: > Hi, > > I frequently do regexp matching with several alternatives where it > does not matter which group matches, e.g. > > md = /foo=(\S+)|bar\[([^]]+)\]/.match input > > For these situations it would be convenient to have something like > this in MatchData > > class MatchData > ��# Return the first capturing group that matched > ��def first_group # or #first but this might collide with Array#first > �� ��for i in 1...size > �� �� ��group = self[i] and return group > �� ��end > �� ��nil > ��end > > ��# Return the last capturing group that matched > ��def last_group # or #last but this might collide with Array#last > �� ��(size - 1).downto 1 do |i| > �� �� ��group = self[i] and return group > �� ��end > �� ��nil > ��end > end > > I propose to add that to the std lib. ��What do others think? Isn't md.captures.first and md.captures.last sufficient? It's not exactly the same functionality, but close, right? I'm not voting, yet, but trying to understand what's new here. Regards, Ammar