>>> text =\
... """xyxyxy testmatch0
... xyxyxy testmatch1
... xyxyxy
... whyisthismatched1
... xyxyxy testmatch2
... xyxyxy testmatch3
... xyxyxy
... whyisthismatched2
... """
>>> re.findall("^\s*xyxyxy\s+([a-z0-9]+).*$", text, re.MULTILINE)
[u'testmatch0', u'testmatch1', u'whyisthismatched1', u'testmatch2', u'testmatch3', u'whyisthismatched2']
所以我的期望是不符合包含“whyithismatched”的行。你知道吗
Python re文档说明了以下内容:
(Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.
我的问题是,这是否真的是预期的行为或一个错误。 如果需要某个人,请解释为什么这些线是匹配的,以及我应该如何修改我的模式以获得我期望的行为:
[u'testmatch0', u'testmatch1', u'testmatch2', u'testmatch3']
就
\s
字符类而言,换行符也是空白。如果要匹配空间只需匹配[ ]
:相关问题 更多 >
编程相关推荐