Python版本低于2.7.5下运行正则报错sre_constants.error: nothing to repeat

来源:互联网 发布:费米估算法 编辑:程序博客网 时间:2024/05/23 01:12

前言

在linux服务器安装配置爬虫相关环境时,python版本是2.7.3,在安装Twisted模块时,安装其依赖包m2r时报错:sre_constants.error: nothing to repeat

解决方案

报错位置在m2r/m2r.py文件下:

rest_role = re.compile(r':.*?:`.*?`|`[^`]+`:.*?:')rest_link = re.compile(r'`[^`]*?`_')inline_math = re.compile(r'`\$(.*)?\$`')eol_literal_marker = re.compile(r'(\s+)?::\s*$')

inline_math = re.compile(r'`\$(.*)?\$`')这一句!
因为python模块re对“*”匹配处理异常导致语句执行失效,导致报错!
所以在Python 版本低于2.7.5时,在遇到需要正则匹配“*”时需要做转换处理!

在stackoverflow找到一个回答:
regex error - nothing to repeat

参考第二个回答,我们将原来的

inline_math = re.compile(r'`\$(.*)?\$`')

替换为;

inline_math = re.compile(r'`\$(.[a-zA-z\s]*)?\$`')

然后重新安装,编译通过,安装成功!

阅读全文
1 0
原创粉丝点击