In the code you posted you are running that match once on every line in the text file. (And you are ignoring the success or failure of that match). You could check the success each time and then exit the loop when you find a match.
Or, if the text file isn't too big, and there's only one place it can match, then don't do it on each line one-at-a-time, just run it on the entire file.
Or, if the text file isn't too big, and there's only one place it can match, then don't do it on each line one-at-a-time, just run it on the entire file.
import re with open('testregpad.txt') as f: match = re.search('event\);\">(.*)<\/a.*', f.read()) if match: print(match.group(1)) else: print("Couldn't find a match in the file.")