T. Onoma
10/22/2004 1:38:00 AM
Better way to find the closest match?
# example data
str = "some string abc xyz"
tokens = [ /abc/, /xyz/ ]
i = 0
# must be a better way?
token,match,mindex = tokens.inject([nil,nil,str.length]){ |memo, tkn|
s = str.index( tkn.start_pattern, i )
if s
s < memo[2] ? [tkn, $~, s] : memo
else
memo
end
}
In this case this should return a match to 'abc' (this case has not been
tested, but the core of this example has)
Thanks,
T.