Saturday, 24 August 2013

re.findall not returning full match? (python 2.7)

re.findall not returning full match? (python 2.7)

I have a file that includes a bunch of strings like "size=XXX;". I am
trying python's re module for the first time and am a bit mystified by the
following behavior: if I use a pipe for 'or' in a regular expression, I
only see that bit of the match returned. E.g.:
>>> myfile = open('testfile.txt','r').read()
>>> print re.findall('size=50;',myfile)
['size=50;', 'size=50;', 'size=50;', 'size=50;']
>>> print re.findall('size=51;',myfile)
['size=51;', 'size=51;', 'size=51;']
>>> print re.findall('size=(50|51);',myfile)
['51', '51', '51', '50', '50', '50', '50']
>>> print re.findall(r'size=(50|51);',myfile)
['51', '51', '51', '50', '50', '50', '50']
The "size=" part of the match is gone. (Yet it is certainly used in the
search, otherwise there would be more results). What am I doing wrong?

No comments:

Post a Comment