Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug with re.split function and re.DOTALL flag in re module of Python 2.7.1

I have a Mac running Lion and Python 2.7.1. I am noticing something very strange from the re module. If I run the following line:

print re.split(r'\s*,\s*', 'a, b,\nc, d, e, f, g, h, i, j, k,\nl, m, n, o, p, q, r')

I get this result:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']

But if I run it with the re.DOTALL flag like this:

print re.split(r'\s*,\s*', 'a, b,\nc, d, e, f, g, h, i, j, k,\nl, m, n, o, p, q, r', re.DOTALL)

Then I get this result:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q, r']

Note that 'q, r' is counted as one match instead of two.

Why is this happening? I don't see why the re.DOTALL flag would make a difference if I am not using dots in my pattern. Am I doing something wrong or is there some sort of bug?

like image 300
Elias Zamaria Avatar asked Nov 11 '11 22:11

Elias Zamaria


1 Answers

>>> s = 'a, b,\nc, d, e, f, g, h, i, j, k,\nl, m, n, o, p, q, r'
>>> re.split(r'\s*,\s*', s)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']
>>> re.split(r'\s*,\s*', s, maxsplit=16)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q, r']
>>> re.split(r'\s*,\s*', s, flags=re.DOTALL)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']

The problem is that you are passing re.DOTALL positionally, where it sets the maxsplit=0 argument, not the flags=0 argument. re.DOTALL happens to be the constant 16.

like image 184
ephemient Avatar answered Nov 04 '22 22:11

ephemient