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

前言

在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]*)?\$`')

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值