[Tutor] re.findall(), but with overlaps?
kent37 at tds.net
Sat Sep 3 06:03:00 CEST 2005
Terry Carroll wrote:
> But he asked me, is there a standard method to get even overlapped
> After looking through the docs, I couldn't find a way to do this in
> standard methods, so I gave him a quick RYO solution:
>>>>def myfindall(regex, seq):
> ... resultlist=
> ... pos=0
> ... while True:
> ... result = regex.search(seq, pos)
> ... if result is None:
> ... break
> ... resultlist.append(seq[result.start():result.end()])
> ... pos = result.start()+1
> ... return resultlist
> ['BAB', 'BEB', 'BIB']
> But just curious; are we reinventing the wheel here? Is there already a
> way to match even overlapping substrings? I'm surprised I can't find one.
AFAIK that is the way to do it. You can shorten it a little by using result.group() instead of seq[result.start():result.end()].
More information about the Tutor