下面这个错误是由于在正则[...]的内部,减号'-'是一个有特殊含义的字符(代表字符范围)
所以如果需要在[...]内匹配减号'-',需要用反斜杠'\'转义.
>>> import re>>> re.split(r'[+\-*/%]','+a-b-c')['', 'a', 'b', 'c'] >>> re.split(r'[+-*/%]','+a-b-c')Traceback (most recent call last): File "", line 1, in re.split(r'[+-*/%]','+a-b-c') File "C:\Python27\lib\re.py", line 167, in split return _compile(pattern, flags).split(string, maxsplit) File "C:\Python27\lib\re.py", line 242, in _compile raise error, v # invalid expressionerror: bad character range