python关于芬德尔奇怪的行为

2024-10-01 19:26:21 发布

您现在位置:Python中文网/ 问答频道 /正文

>>> 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']

Tags: thetextrenewlineanythismatchescharacter
1条回答
网友
1楼 · 发布于 2024-10-01 19:26:21

\s字符类而言,换行符也是空白。如果要匹配空间只需匹配[ ]

>>> re.findall("^\s*xyxyxy[ ]+([a-z0-9]+).*$", text, re.MULTILINE)
[u'testmatch0', u'testmatch1', u'testmatch2', u'testmatch3']

相关问题 更多 >

    热门问题