re.findall() 未返回所有匹配项

re.findall() not returning all matches

      import re
      text = 'fruits to be sold are apple orange and peach'
      x = re.findall(r'fruits.*(apple|orange|peach).*',text,re.I)
      print(x)

代码的objective是return一个列表,里面是'fruits'.

后面句子中的水果名称

所以预期的结果应该是这样的

['apple','orange','peach']

但我只得到 sentence.i.e 桃子中的最后一场比赛。 谁能帮我看看哪里出错了?

您可以将正则表达式替换为更简单的

x = re.findall(r'(apple|orange|peach)',text,re.I)