I have a script that takes in an argument and tries to find a match using regex. On single values, I don't have any issues, but when I pass multiple words, the order matters. What can I do so that the regex returns no matter what the order of the supplied words are? Here is my example script:
import re
from sys import argv
data = 'some things other stuff extra words'
pattern = re.compile(argv[1])
search = re.search(pattern, data)
print search
if search:
print search.group(0)
print data
So based on my example, if I pass "some things" as an arg, then it matches, but if i pass "things some", it doesn't matches, and I would like it to. Optionally, I would like it to also return if either "some" or "things" match.
The argument passed could possibly be a regex
itertools.permutations()to generate all possible orderings of the words indata, then call your regexp on each one.re.compile("|".join(argv[1:]))? You can pass as many as you want.