Search code examples
pythonregexregular-language

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)

The objective of the code is to return a list, containing names of the fruits in the sentence after the word 'fruits'.

so the expected result should be like

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

but instead am getting only the last match in the sentence.i.e peach. Could someone help me out where did I go wrong ?


Solution

  • You could replace your regexp by the simpler

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