Why do we use re.compile() method in Python regular expression?

The re.compile() method

re.compile(pattern, repl, string):

 We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it.


import re
result=pattern.findall('TP Tutorialspoint TP')
print result
result2=pattern.findall('TP is most popular tutorials site of India')
print result2


['TP', 'TP']

Updated on: 02-Nov-2023

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started