雖然我已經適當地設置了表達式,但拆分不按預期工作。Python積極lookbehind拆分可變寬度
c = re.compile(r'(?<=^\d\.\d{1,2})\s+');
for header in ['1.1 Introduction', '1.42 Appendix']:
print re.split(c, header)
預期結果:
['1.1', 'Introduction']
['1.42', 'Appendix']
我正在以下堆棧跟蹤:
Traceback (most recent call last):
File "foo.py", line 1, in
c = re.compile(r'(?<=^\d.\d{1,2})\s+');
File "C:\Python27\lib\re.py", line 190, in compile
return _compile(pattern, flags)
File "C:\Python27\lib\re.py", line 242, in _compile
raise error, v # invalid expression
sre_constants.error: look-behind requires fixed-width pattern
<<< Process finished. (Exit code 1)
錯誤消息說它 - 你不能在python的正則表達式引擎中有可變長度的lookaround。 – roippi
查看[regex](https://pypi.python.org/pypi/regex)模塊,該模塊允許可變長度lookbehind。 – BrenBarn