import re with open('app/modules/sag/templates/detail.html', 'r', encoding='utf-8') as f: text = f.read() s = text.find('async function saveTime()') if s != -1: e = text.find('async function createTodoStep', s) if e == -1: e = s + 2000 print(text[s:e])