小编Nay*_*ore的帖子

Python 正则表达式错误:后视需要固定宽度的模式

下面的正则表达式应该匹配任何:text:多数民众赞成由preceeded start-of-stringwhitespace或者:,和成功end-of-stringwhitespace:(连同一些额外的规则)

我不擅长正则表达式,但我在 regexr.com 中提出了所需的解决方案:

(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)
Run Code Online (Sandbox Code Playgroud)
:match1::match2: :match3:
:match4:
000:matchNot:
:matchNot:000
:match Not:
Run Code Online (Sandbox Code Playgroud)

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

但是在 Python 3 上,这会引发错误。

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

re.error: 后视需要固定宽度的模式

任何人都知道解决此问题的好方法?任何提示表示赞赏。

regex python-3.x

4
推荐指数
2
解决办法
2476
查看次数

标签 统计

python-3.x ×1

regex ×1