【发布时间】:2014-05-09 23:57:15
【问题描述】:
虽然我已经适当地设置了表达式,但是拆分没有按预期工作。
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']
我得到以下堆栈跟踪:
回溯(最近一次通话最后一次):
中的文件“foo.py”,第 1 行 c = re.compile(r'(? 编译中的文件“C:\Python27\lib\re.py”,第 190 行
返回 _compile(模式,标志)
_compile
中的文件“C:\Python27\lib\re.py”,第 242 行 引发错误,v # 表达式无效
sre_constants.error:后视需要固定宽度的模式
【问题讨论】:
-
错误消息说 - 你不能在 python 的正则表达式引擎中进行可变长度环视。
-
看看regex 模块,它允许可变长度的后视。
标签: python regex string split lookbehind