Python Tutorial
Python RegEx Functions - findall, search, split and sub
What are RegEx Functions in Python?
The re module provides a collection of methods for searching for a match in a string. Here is the list of RegEx functions in Python along with their description:
findall
It returns a list of all matches.
Code:
import re
text = "rain in jodhpur"
x = re.findall("ai", text)
print(x)
Output:
['ai', 'ai']
search
It returns a list containing all matches.
Code:
import re
text = " rain in jodhpur"
x = re.search("\s", text)
print( x.start())
Output:
3
split
It returns a list with the string split at each match.
Code:
import re
text = "rain in jodhpur"
x = re.split("\s", text)
print(x)
Output:
['rain', 'in', ‘jodhpur']
sub
This function replaces a string with one or more matches.
Code:
import re
text = "rain in jodhpur"
x = re.sub("\s", "9", text)
print(x)
Output:
rain9in9jodhpur