Louis J Scoras
10/19/2006 5:32:00 PM
You should also be able to do this with split. Then you can use
strings as well as regexes:
class String
def match_between(head, tail)
strings = []
s = split(head, 2)[1]
while (s)
match, rest = s.split(tail,2)
return strings unless rest
strings << match
s = rest.split(head, 2)[1]
end
strings
end
end
So with the same string as before,
str = "I just wanted to say hello to you before I say goodbye for
the final time."
str << " Oh, hello and goodbye again."
you get:
p str.match_between(/hello/,/goodbye/)
# => [" to you before I say ", " and "]
and also
p str.match_between('a','e')
# => ["nt", "y h", "y goodby", "l tim", "nd goodby"]
I'm sure you could also tweek it to operate in a manner which doesn't
consume the rest of the string, i.e.: the 'g' flag.
--
Lou.