为什么python的re.findall不返回我的示例中找到的所有子字符串?

2024-06-08 03:15:23 发布

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

Possible Duplicate:
Python regex find all overlapping matches?

我不明白为什么python的re.findall不返回下面示例中找到的所有子字符串。有什么想法吗?

>>> import re
import re
>>> t='1 2 3'
t='1 2 3'
>>> m=re.findall('\d\s\d',t)
m=re.findall('\d\s\d',t)
>>> m
m
['1 2']

但预期的结果是 m=['12','23']。

有关信息,我使用的是Python2.6.1。谢谢。


Tags: 字符串importre信息示例allfindregex
1条回答
网友
1楼 · 发布于 2024-06-08 03:15:23

help(re.findall)

Help on function findall in module re:

findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.

If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern has more than one group.

Empty matches are included in the result.

由于两个结果重叠(都有“2”在其中),因此只返回第一个结果。

如果你有t='1 2 3 4',结果将是['1 2', '3 4']

相关问题 更多 >

    热门问题