You can use the below-mentioned code as well from module re.py:-
def match(pattern, string, flags=0):
return _compile(pattern, flags).match(string)
def _compile(*key):
cachekey = (type(key[0]),) + key
p = _cache.get(cachekey)
if p is not None:
return p
if len(_cache) >= _MAXCACHE:
_cache.clear()
_cache[cachekey] = p
return p
To know more about this you can have a look at the following video tutorial:-