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

后端 未结 1 1148
执念已碎
执念已碎 2021-01-12 11:52

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*         


        
相关标签:
1条回答
  • 2021-01-12 12:30
    >>> 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.

    0 讨论(0)
提交回复
热议问题