gpt4 book ai didi

python - re.VERBOSE 和先行断言错误

转载 作者:行者123 更新时间:2023-11-28 21:39:09 27 4
gpt4 key购买 nike

我有一个冗长的 (re.X) 标记的正则表达式抛出异常,即使它看起来等同于它的压缩版本。 (我从后者构建了前者。)

精简版:

import re
test = 'catdog'
test2 = 'dogcat'
pat = re.compile(r'(?=\b\w{6}\b)\b\w*cat\w*\b')

print(pat.search(test))
print(pat.search(test2))
# catdog Match object
# dogcat Match object

详细版本:

pat = re.compile(r"""(               # Start of group (lookahead); need raw string
?= # Positive lookahead; notation = `q(?=u)`
\b\w{6}\b # Word boundary and 6 alphanumeric characters
) # End of group (lookahead)
\b\w*cat\w*\b # Literal 'cat' in between 0 or more alphanumeric""", re.X)
print(pat.search(test).string)
print(pat.search(test2).string)

# Throws exception
# error: nothing to repeat at position 83 (line 2, column 22)

这是什么原因造成的?我找不到扩展版本违反 re.X/re.VERBOSE 的任何条件的原因。来自文档:

This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when in a character class or when preceded by an unescaped backslash. When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.

据我所知,没有以未转义的反斜杠开头的字符类或空格。

最佳答案

这是 Python issue 15606 . re 在详细模式下 token 内有空格的行为与文档不匹配。您不能在 (?= 的中间放置空格。

关于python - re.VERBOSE 和先行断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com