[英]Why does \w+ match a trailing newline?
I am curious why the following would output that there was a match:我很好奇为什么下面的 output 会有匹配:
import re
foo = 'test\n'
match = re.search('^\w+$', foo)
if match == None:
print "It did not match"
else:
print "Match!"
The newline is before the end of the string, yes?换行符在字符串末尾之前,是吗? Why is this matching?
为什么会这样匹配?
From Python's re
documentation.来自 Python 的
re
文档。
'$'
'$'
Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline.匹配字符串的结尾或字符串结尾的换行符之前,并且在 MULTILINE 模式下也匹配换行符之前。 foo matches both 'foo' and 'foobar', while the regular expression foo$ matches only 'foo'.
foo匹配 'foo' 和 'foobar',而正则表达式foo$只匹配 'foo'。 More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches 'foo2' normally, but 'foo1' in MULTILINE mode;
更有趣的是,在 'foo1\nfoo2\n' 中搜索foo.$通常匹配 'foo2',但在 MULTILINE 模式下搜索 'foo1'; searching for a single $ in 'foo\n' will find two (empty) matches: one just before the newline, and one at the end of the string.
在 'foo\n' 中搜索单个$将找到两个(空)匹配项:一个在换行符之前,一个在字符串末尾。
^
and $
mean "start of line" and "end of line", not "start of string" and "end of string". ^
和$
表示“行首”和“行尾”,而不是“字符串开头”和“字符串结尾”。 Use \A
for "start of string" and \Z
for "end of string".使用
\A
表示“字符串的开头”,使用\Z
表示“字符串的结尾”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.