fork download
  1. import re
  2. text = "These are my food preferences mango and I also like bananas and I like grapes too."
  3. pattern = r"(?P<Start>\bpreferences\b)(?P<Mid>(?:\s+\w+(?:\s+\w+){0,6}?\s+like)+)(?:\s+(?P<Last>\w+(?:\s+\w+){1,7}))?"
  4. match = re.search(pattern, text)
  5. if match:
  6. print(match.group("Start"))
  7. print( re.split(r"\s*\blike\b\s*", match.group("Mid").strip()) )
  8. print(match.group("Last"))
Success #stdin #stdout 0.03s 9740KB
stdin
Standard input is empty
stdout
preferences
['mango and I also', 'bananas and I', '']
grapes too