[Solved] Extract text with conditions in Python


Try with this: \d+\s*((?:Apple|Banana|Orange|Pineapple)s?\b[\s\S]*?)(?=$|\d+\s*(?:Apple|Banana|Orange|Pineapple)s?\b)

See:

Regex demo

The code:

import re

regex = r"\d+\s*((?:Apple|Banana|Orange|Pineapple)s?\b[\s\S]*?)(?=$|\d+\s*(?:Apple|Banana|Orange|Pineapple)s?\b)"

test_str = "I have 2 apples in my bag and apples are great food toeat. you shud eat apples daily. it is very good for health. 3 bananas are also good. it reduces fat."

matches = re.findall(regex, test_str, re.MULTILINE | re.IGNORECASE)

for match in matches: print(match + "\n")

code demo

Note: I’m assuming you would want to stop matching on new line characters. If that is not the case, you may remove the re.MULTILINE flag so that $ matches end of string.

1

solved Extract text with conditions in Python