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

PythonServer Side ProgrammingProgramming

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.

Example

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

Output

['TP', 'TP']
['TP']
raja
Published on 11-Jan-2018 07:01:00
Advertisements