From: Dominik Bathon Date: 2005-04-22T10:50:38+09:00 Subject: Re: Linear complete variation of MatchData#to_a, possible? On Wed, 20 Apr 2005 22:59:33 +0200, Trans wrote: > Amazing Pit Capitain! Your hunch was right on the money. Not only was > your suggestion key to the solution, but the output was just as you > expected. Here's the solution I found. It's not all that elegant, but > it appears to work okay. I am certain there are much better solutions > to be had, so if anyone has one to offer... Here is my recursive version: class MatchData def matchtree(index=0) ret=[] b, e=self.begin(index), self.end(index) while (index+=1)<=length if index==length || (bi=self.begin(index))>=e # we are finished, if something is left, then add it ret << string[b, e-b] if e>b break else if bi>=b ret << string[b, bi-b] if bi>b ret << matchtree(index) b=self.end(index) end end end ret end def matchset [pre_match, matchtree, post_match] end end md = /(bb)(cc(dd))(ee)/.match "XXaabbccddeeffXX" p md.to_a p md.matchset md.length.times { |i| p md.matchtree(i) } Output: ["bbccddee", "bb", "ccdd", "dd", "ee"] ["XXaa", [["bb"], ["cc", ["dd"]], ["ee"]], "ffXX"] [["bb"], ["cc", ["dd"]], ["ee"]] ["bb"] ["cc", ["dd"]] ["dd"] ["ee"] Btw. your version seems to have a little problem: > #=> ["XXaa", [["bb"], ["cc", ["dd"]], "ee"], "ffXX"] There are no brackets around the "ee". Dominik