compile():
- A function belongs to “re” module.
- It is used to construct the “pattern object”.
- When we use a pattern repeatedly to extract the data, it is recommended to construct the pattern object using compile().
import re
line = "Python and R"
r1 = re.match(r'and' , line)
print("Match :",r1)
r2 = re.search(r'and' , line)
print("Search :",r2)
r3 = re.split(r'and' , line)
print("split :",r3)
- In the above example, we use ‘and’ pattern repeatedly.
- We construct the pattern object using compile() as follows.
import re
line = "Python and R"
pattern = re.compile('and')
r1 = pattern.match(line)
print("Match :",r1)
r2 = pattern.search(line)
print("Search :",r2)
r3 = pattern.split(line)
print("split :",r3)