If you are using the same regular expression multiple times, it is recommended to compile it for improved performance.

compiled_re = re.compile(r'some_regexpr') for word in text: match = comp.search(compiled_re)) # do something with the match

E.g., if we want to check if a string ends with a substring: