# MatchData matchset and matchtree

**URL:** <https://www.ruby-forum.com/t/matchdata-matchset-and-matchtree/92076>\
**Category:** Ruby\
**Created:** [April 15, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/matchdata-matchset-and-matchtree/92076 "2007-04-15T20:02:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 15, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/matchdata-matchset-and-matchtree/92076/1 "2007-04-15T20:02:44Z")

</div>

I think someone asked about/suggested something like this recently. I  
think these two methods would make a pretty good addition to the core  
library.

class MatchData

# Returns [pre\_match, matchtree, post\_match]. (see matchtree)

# 

# md = /(bb)(cc(dd))(ee)/.match “XXaabbccddeeffXX”

# md.to\_a #=\> [“XXaabbccddeeffXX”, “bb”, “ccdd”, “dd”, “ee”]

# md.matchset #=\> [“XXaa”, [[“bb”], [“cc”, [“dd”]], “ee”],

“ffXX”]

# 

def matchset  
[pre\_match, matchtree, post\_match]  
end

# An alternate to #to\_a which returns the matches in

# order corresponding with the regular expression.

# 

# md = /(bb)(cc(dd))(ee)/.match “XXaabbccddeeffXX”

# md.to\_a #=\> [“XXaabbccddeeffXX”, “bb”, “ccdd”, “dd”, “ee”]

# md.matchtree #=\> [[“bb”], [“cc”, [“dd”]], “ee”]

# 

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  
return ret  
end

end

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 18, 2007, 9:57am UTC](https://www.ruby-forum.com/t/matchdata-matchset-and-matchtree/92076/2 "2007-04-18T09:57:13Z")

</div>

Hi,

Moved to ruby-core.

At Mon, 16 Apr 2007 03:01:46 +0900,  
Trans wrote in [ruby-talk:248025]:

> I think someone asked about/suggested something like this recently. I  
> think these two methods would make a pretty good addition to the core  
> library.

Seems nice, but I’m not sure if the names are best for them.  
Any other idea?

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 19, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/matchdata-matchset-and-matchtree/92076/3 "2007-04-19T18:50:25Z")

</div>

On 4/18/07, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Seems nice, but I’m not sure if the names are best for them.  
> Any other idea?

Can’t think of anything better, myself. But certainly feel free to  
name them whatever seems more appropriate.

Thanks,  
T.
