from contextlib import contextmanager

@contextmanager
def flag_regexen(flag):
    import re
re.my_compile = re.compile
re.compile = lambda pattern, flags: re.my_compile(pattern, flags | flag)
yield
re.compile = re.my_compile
Using a global flag for python RegExp compile