with open('src/main/resources/templates/doctor.html', 'r', encoding='utf-8') as f: text = f.read() import re matches = re.finditer(r'.{0,100}chuyên khoa.{0,100}', text) for m in matches: print("Match:", m.group(0))