Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Is there any benefit in using compile for regular expressions in Python?

h = re.compile('hello') 

h.match('hello world')

vs

re.match('hello', 'hello world')

1 Answer

0 votes
by (106k points)

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:-

Browse Categories

...