0
点赞
收藏
分享

微信扫一扫

Python中 re模块 正则表达式 零宽度匹配


程序出错 re.error: look-behind requires fixed-width pattern  代码报出错误

拿 "(?<!pattern)" 这个为例子

(?<!pattern)

反向否定预查,与正向否定预查类似,只是方向相反。例如"​​(?<!95|98|NT|2000)Windows​​​"能匹配"​​3.1Windows​​​"中的"​​Windows​​​",但不能匹配"​​2000Windows​​​"中的"​​Windows​​"。

re.findall("(?<!95|98|NT|2000)Windows", "1232320Windows")

上面代码执行会报出 

re.error: look-behind requires fixed-width pattern

在Python中这个意思就是你需要将你的候选项改为 相同位数才行

re.findall("(?<!95|98|NT|20)Windows", "1232320Windows")

这样写就符合结构 但是缺少关键字

解决方式:

​​regex · PyPIAlternative regular expression module, to replace re.https://pypi.org/project/regex/​​

pip install regex

可以通过安装 regex 增强 re 包

import regex as re

print(re.findall("(?<!95|98|NT|2000)Windows", "1232320Windows"))


# 输出
# ['Windows']

举报

相关推荐

0 条评论