Импорт:
import re
Входные данные:
rules = {
"TAG1": {"A", "B", "C"},
"TAG2": {"1", "2", "3"}
}
template = "foo[TAG1][TAG2]"
test_strings = ["foo[A][1]", "foo[B][3]", "foo[2][B]", "foo[A][A]", "foo[1][3]", "foo[C][B]", "foo[C]", "foo[23]"]
Код:
compiled_template = template[:]
for k, v in rules.items():
if k in template:
compiled_template = compiled_template.replace(f"[{k}]", f"(?=.*\[({'|'.join(v)})\])")
for string in test_strings:
if re.match(compiled_template, string):
print(string)
Выход:
foo[A][1]
foo[B][3]
foo[2][B]