匹配字符串并将所有结果放在Python中的列表中

时间:2013-05-22 02:34:08

标签: python regex

我要匹配字符串中的内容,我想将所有匹配的字符串放入列表中;我该怎么做?

现在我正在使用这个

myStr = 'one two one three'
match = re.match(r'one', myStr)

但是当我使用match打印变量print m.group()的内容时,它只显示第一次出现。我希望得到所有出现的内容,然后将所有这些值放入列表中。

1 个答案:

答案 0 :(得分:2)

>>> import re
>>> re.findall(r'one', 'one two one three')
['one', 'one']