Different number of matches from re.findall and re.split

Arnaud Delobelle arnodel at googlemail.com
Mon Jan 11 16:56:19 CET 2010


On 11 Jan, 15:35, Jeremy <jlcon... at gmail.com> wrote:
> Hello all,
>
> I am using re.split to separate some text into logical structures.
> The trouble is that re.split doesn't find everything while re.findall
> does; i.e.:
>
>
>
> > found = re.findall('^ 1', line, re.MULTILINE)
> > len(found)
>    6439
> > tables = re.split('^ 1', line, re.MULTILINE)
> > len(tables)
> > 1
>
> Can someone explain why these two commands are giving different
> results?  I thought I should have the same number of matches (or maybe
> different by 1, but not 6000!)
>
> Thanks,
> Jeremy

When in doubt, the documentation is a good place to start :)

http://docs.python.org/library/re.html#re.split

    re.split(pattern, string[, maxsplit=0])

http://docs.python.org/library/re.html#re.findall

    re.findall(pattern, string[, flags])

Notice that re.split's optional third argument is not for passing
flags.

HTH

--
Arnaud





More information about the Python-list mailing list