Python正则表达式错误:回溯需要固定宽度模式。

8
以下正则表达式可以匹配以字符串开始空格:开头,以字符串结束空格:结尾的任何:text:(以及一些其他规则)。
我不太擅长正则表达式,但是我已经在regexr.com上找到了所需的解决方案。
(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)

:match1::match2: :match3:
:match4:
000:matchNot:
:matchNot:000
:match Not:

结果: :match1:, :match2:, :match3:, :match4:

但是在Python 3中会引发错误。

re.search("(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)", txt)

re.error: look-behind需要一个固定宽度的模式

有人知道解决这个问题的好方法吗?如果有任何提示,将不胜感激。


请确保使用(?<![^\s:]):[^\s:]+:(?![^\s:])而不是(?:^|(?<=[\s:]))(:[^\s:]+:)(?=[\s:]|$) - Wiktor Stribiżew
2
这个回答解决了你的问题吗?Python正则表达式引擎 - "look-behind requires fixed-width pattern"错误 - oguz ismail
3个回答

11
可能最简单的解决方案是使用新的 regex 模块,该模块支持无限回顾后发断言:
import regex as re

data = """:match1::match2: :match3:
:match4:
000:matchNot:
:matchNot:000
:match Not:"""

for match in re.finditer("(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)", data):
    print(match.group(0))

这将产生:
:match1:
:match2:
:match3:
:match4:

这绝对比分割后顾之忧容易得多。当然,如果你可以使用不同的库。 - Justas
这刚刚让我从漫长的模式中解脱出来 :) - Frankie Drake

6
在Python中,您可以使用以下方法来避免此错误:
(?:^|(?<=[\s:]))(:[^\s:]+:)(?=[\s:]|$)

锚点符号^$本身就是零宽匹配器。 正则表达式演示

1
另一个选项是安装regex:
$ pip3 install regex

然后,我们将编写一些表达式,并使用(*SKIP)(*FAIL)来跳过我们不想出现的模式:
import regex as re

expression = r'(?:^\d+:[^:\r\n]+:$|^:[^:\r\n]+:\d+$|^(?!.*:\b\S+\b:).*$)(*SKIP)(*FAIL)|:[a-z0-9]+:'
string = '''
:match1::match2: :match3:
:match4:
000:matchNot:
:matchNot:000
:match Not:

'''

print(re.findall(expression, string))

输出

[':match1:', ':match2:', ':match3:', ':match4:']

如果您希望简化/修改/探索表达式,则已在regex101.com的右上方面板上进行了解释。如果您愿意,您还可以观看this link,了解它如何匹配一些示例输入。

正则表达式电路

jex.im 可视化正则表达式:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接