I have a dictionary who's keys are all precompiled regular expressions. I want to match a string to any of these regular expressions.
When researching I found that you can match multiple regular expressions by joining them with the join method. But when I do so, I get a Type error:
import re
regex1 = re.compile("regex1.*")
regex2 = re.compile("regex2\d")
re_dict = {regex1 : "stuff", regex2 : "otherstuff"}
match_multiple = "|".join(list(re_dict.keys()))
string = 'regex25'
if re.match(match_multiple, string):
print("matched")
This gives:
Traceback (most recent call last):
File "./a.py", line 7, in <module>
match_multiple = "|".join(list(re_dict.keys()))
TypeError: sequence item 0: expected str instance, re.Pattern found