| Class | MatchData |
| In: |
lib/core/facets/matchdata/matchset.rb
lib/core/facets/matchdata/match.rb |
| Parent: | Object |
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"]
CREDIT: Trans
# File lib/core/facets/matchdata/matchset.rb, line 11
11: def matchset
12: [pre_match, matchtree, post_match]
13: 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"]
CREDIT: Trans
# File lib/core/facets/matchdata/matchset.rb, line 24
24: def matchtree(index=0)
25: ret=[]
26: b, e=self.begin(index), self.end(index)
27: while (index+=1)<=length
28: if index==length || (bi=self.begin(index))>=e
29: # we are finished, if something is left, then add it
30: ret << string[b, e-b] if e>b
31: break
32: else
33: if bi>=b
34: ret << string[b, bi-b] if bi>b
35: ret << matchtree(index)
36: b=self.end(index)
37: end
38: end
39: end
40: return ret
41: end