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

Tim Chase python.list at tim.thechases.com
Tue Jan 12 21:52:30 EST 2010


Steve Holden wrote:
> Steve Holden wrote:
> [...]
>>> 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!)
>>>
>> re.MULTLINE is apprently 1, and you are providing it as the "maxsplit"
>> argument. Check the API in the documentation.
>>
> Sorry, I presume re.MULTILINE must actually be zero for the result of
> re,split() to be of length 1 ...

Because it's not doing a multiline split and it's anchored at the 
beginning of the line, it only returns one result (there's 
nothing before the start-of-line to return as the left-side of 
the split):

 >>> import re
 >>> re.MULTILINE
8
 >>> s = """
... abc
... def
... abc
... def"""
 >>> re.split('^', s,  re.MULTILINE)
['\nabc\ndef\nabc\ndef']
 >>> re.split('b', s,  re.MULTILINE)
['\na', 'c\ndef\na', 'c\ndef']
 >>> re.split('b', 'ab'*10,  re.MULTILINE)
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'abab']


But your original logic is sound...the 3rd argument re.split() is 
"maxsplit" not "flags", and if you want to use flags with 
.split() you have to either specify it within the regexp or by 
compiling the regexp and using the resulting compiled object as 
detailed elsewhere in the thread by MRAB and Duncan.

-tkc






More information about the Python-list mailing list