I would like to write a regex for searching for the existence of some words, but their order of appearance doesn\'t matter.
For example, search for \"Tim\" and \"st
You can use Positive Lookahead to achieve this. The lookahead approach is nice for matching strings that contain both substrings regardless of order.
pattern = re.compile(r'^(?=.*Tim)(?=.*stupid).*$')
Example:
>>> s = '''Hey there stupid, hey there Tim
Hi Tim, this is stupid
Hi Tim, this is great'''
...
>>> import re
>>> pattern = re.compile(r'^(?=.*Tim)(?=.*stupid).*$', re.M)
>>> pattern.findall(s)
# ['Hey there stupid, hey there Tim', 'Hi Tim, this is stupid']