在Python3中查找字符串中所有出现的单词

Vin*_*Vin 2 python regex python-3.x

我试图在1个句子中找到包含"地狱"的所有单词.下面的字符串中有3次出现.但是re.search只返回前两次出现.我试过findall和搜索.有人可以告诉我这里有什么问题吗?

>>> s = 'heller pond hell hellyi'
>>> m = re.findall('(hell)\S*', s)
>>> m.group(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'group'
>>> m = re.search('(hell)\S*', s)
>>> m.group(0)
'heller'
>>> m.group(1)
'hell'
>>> m.group(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: no such group
>>> 
Run Code Online (Sandbox Code Playgroud)

iCo*_*dez 5

您可以在任一侧使用re.findall和搜索hell零个或多个单词字符:

>>> import re
>>> s = 'heller pond hell hellyi'
>>> re.findall('\w*hell\w*', s)
['heller', 'hell', 'hellyi']
>>>
Run Code Online (Sandbox Code Playgroud)