In the case of python regex, it is handled by the ‘re’ module using various functions and constants. It is imported using the following command,
import re
Some of the functions defined in the ‘re’ module are,
1) findall( )
2) search( )
3) split( )
4) sub( )
5) Match object
1) findall( )
All matches are returned in a list.
import re
text = "Life is good"
y = re.findall("fe", text)
print(y)
The output is [‘fe’]. If no match returns an empty list.
2) search( )
The search( ) function returns the match as an object(match object) if a match is found for the given pattern. If no match is found, it returns none.
import re
text = "Life is good"
y = re.search("fe", text)
if y:
print("Match found")
else :
print("No match found")
The output is ‘Match found’.
3) split( )
Returns the list of strings where splitting occurred. If a split pattern is not found, return the original string in the list.
import re
text = "Life is good"
y = re.split("\s", text)
print(y)
The output is ['Life', 'is', 'good']
4) sub( )
Replaces or substitutes the matched pattern with the given string.
import re
text = "The rain in Spain"
y = re.sub("i", "9", text)
print(y)
The output is ‘The ra9n 9n Spa9n’
5) Match Object
A match object is returned by a search( ) function. The match object contains the information of that particular search.
import re
text = "Life is good"
y = re.search("fe", text)
print(y)
The output is <re.Match object; span=(2, 4), match='fe'>
Some of match object methods are .span( ), .string, .group( ) etc
import re
#Search for an raw string "d" character in the given words, and print its starting and ending position of first occurrence:
text = "Life is good"
y = re.search(r"d", text)
print(y.span())
The output is (11, 12).
import re
#Returns the string in which searching:
text = "Life is good"
y = re.search(r"i", text)
print(y.string)
The output is Life is good
import re
#Search for an upper case "G" character in the beginning of a word, and returns the word:
text = "Life is Good"
y = re.search(r"\bG\w+", text)
print(y.group())
The output is ‘Good’.
We have discussed above the most common regular expressions and the common methods defined in the python ‘re’ module.