MatchData matchset and matchtree

T

Trans

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top