简体   繁体   English

为什么 \w+ 匹配尾随换行符?

[英]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.

相关问题 open('.....", 'w+') 不创建新文件 - open('....", 'w+') does not create a new file 为什么python文件描述符“ w +”用于读取时会擦除文件内容? - Why does the python file descriptor 'w+' erase the contents of file when used for reading? 为什么 read() 在 open() 中不能使用 'w+' 或 'r+' 模式 - Why does read() not work with 'w+' or 'r+' mode in open() function python 为什么更改为“w+”模式同时读取和写入文件会导致读取失败? - Why does changing to `w+` mode for simultaneous reading from and writing to a file cause the read to fail? 正则表达式匹配(\\ w +)以捕获由|||分隔的单个单词 - Python - Regex match (\w+) to capture single words delimited by ||| - Python open(“text.txt”,“w+”) 不创建新文件 - open(“text.txt”,“w+”) does not create a new file pytube.exceptions.RegexMatchError: __init__: 找不到匹配的 ^\w+\W - pytube.exceptions.RegexMatchError: __init__: could not find match for ^\w+\W 为什么Python pexpect与此处的换行符不符合预期的提示 - Why Python pexpect does not match the expected prompt with newline here 我无法创建一个不存在的文件并使用 w+ 模式写入它 - I am unable to create a file that does not exist and write to it using w+ mode python 有没有结合“w+”和“r”特性的文件打开方式? - Does python have a file opening mode which combines the features of “w+” and “r”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM