0

Intro: I am trying to extract surrounding words next to a key word of choice. I've learned how to do this using re.compile. Other threads address this issue, e.g.: Finding words after keyword in python

Problem: My issues is that what if keyword appears more than one time in a string. e.g., my keyword is "pie" and my string (s1) is: "i like to eat blueberry pie it is delicious but i also like to eat apple pie it is cool."

How do I extract "blueberry", "it", "apple" and "it"? My current method only extracts "blueberry" and "it."

Current method:

re.compile(r'((?:\w+(?:\s+|$)){1})+pie\s+((?:\w+(?:\s+|$)){1})').findall(s1)

Thanks let me know if you need clarification!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

0 Answers0