-1

Possible Duplicate:
Python regex find all overlapping matches?

I don't see why python's re.findall does not return all the found substrings in my example below. Any ideas?

>>> import re
import re
>>> t='1 2 3'
t='1 2 3'
>>> m=re.findall('\d\s\d',t)
m=re.findall('\d\s\d',t)
>>> m
m
['1 2']

But the expected result is m = ['1 2', '2 3'].

For info, I am using python 2.6.1. Thanks.

Community
  • 1
  • 1
zell
  • 9,830
  • 10
  • 62
  • 115

1 Answers1

5

help(re.findall)

Help on function findall in module re:

findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.

If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern has more than one group.

Empty matches are included in the result.

Since the two results overlap (both have the '2' in them) only the first one will be returned.

if instead you will have t='1 2 3 4' the result will be ['1 2', '3 4'].

Itay Karo
  • 17,924
  • 4
  • 40
  • 58