- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个冗长的 (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/
上周在.NET性能优化群里面有一个很有意思的讨论,讨论的问题如下所示: 请教大佬:2D数组,用C#先遍历行再遍历列,或者先遍历列再遍历行,两种方式在性能上有区别吗? 据我所知,
访问链接树的所有节点的最佳方法是什么(所有节点都引用父节点和所有子节点,根节点的父节点为 null),以便在其任何祖先之前都不会访问任何节点?非递归的布朗尼点数。 最佳答案 伪代码: NodesToV
我是一名优秀的程序员,十分优秀!