You could do it like this if it's one string and want 5 word before
diagnosis
and 5 word after.>>> import re >>> s1 = 'Hey doctor Who is here - I will add R45.1 as diagnosis, then some other medical terms and stuff about the patient. ffdsas ,dsfd tsdsf' >>> s2 = 'Some other medical terms and stuff diagnosis of R45.2 was entered for this patient. Where did Doctor Who go? Then xxx feea fdsfd' >>> r1 = re.search(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}diagnosis(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", s1) >>> r2 = re.search(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}diagnosis(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", s2) >>> r1.group() 'I will add R45.1 as diagnosis, then some other medical terms' >>> r2.group() 'other medical terms and stuff diagnosis of R45.2 was entered for'