From: Trans Date: 2005-04-21T05:59:33+09:00 Subject: Re: Linear complete variation of MatchData#to_a, possible? 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... class MatchData def matchset b = Hash.new(0) e = Hash.new(0) self.captures.size.times do |i| b[self.begin(i)] += 1 e[self.end(i)] += 1 end a = self.string.split(//) c = "" ca = [] stack = [] (a.size).times { |i| # end if e[i] and e[i] != 0 ca << c e[i].times { ca = stack.pop } c = nil end # begin if b[i] and b[i] != 0 ca << c if c b[i].times { stack << ca ca << [] ca = ca.last } c = nil end # content c = "" unless c c << a[i] #if a[i] } ca << c return ca end end #class Matchdata md = /(bb)(cc(dd))(ee)/.match "XXaabbccddeeffXX" p md.to_a p md.matchset #=> ["XXaa", [["bb"], ["cc", ["dd"]], "ee"], "ffXX"]