Regular Expression Functions
Regular Expression is a text string used to describe search patterns.
re module: It’s a python built-in package that is used to work with Regular Expressions.
Examples :
- Nlp: RegEx can search for the NLP string within other strings.
- [a-g]: it can search all single characters between a to g.
- [0-7]+: It will return all numbers between 0 to 7 with more than one number.
Some of the commonly used RegEx Functions are :
1. Search() :
This method is used to search the required pattern from the given string.
If the Patten is present it shows the match or else, it returns none.
Example :
import resentence1 = “Look at the sky. We are not alone. The whole universe is friendly to us and conspires only to give the best to those who dream and work.”search_func = re.search(r’conspires’, sentence1)
print(search_func)
Output :
<re.Match object; span=(76, 85), match=’conspires’>
2. Match() :
The match() method will search the regex pattern only at the beginning of the string.
Example :
import reString1 = “Look at the sky. We are not alone. The whole universe is friendly to us and conspires only to give the best to those who dream and work.”
String2 = “My birthday is on 25th May”Pattern1 =’Look’
Pattern2 = ‘May’print(re.match(Pattern1, String1, re.IGNORECASE))
print(re.match(Pattern2, String2, re.IGNORECASE))
Output :
<re.Match object; span=(0, 4), match='Look'>
None
3. Fullmatch() :
This method returns the match object only if the pattern matches the entire string.
Example :
import reprint(re.fullmatch(‘[a-z0–9]+’, ‘hello2021’))
Output :
<re.Match object; span=(0, 9), match='hello2021'>
4. Split() :
It is used to split the string into a list of substrings by the occurrences of the regex pattern.
Example :
test = “Look at the sky…….. We are not alone. The whole universe is>>>>>>>friendly to us and conspires*only to give the best to those who dream and work”re.split(‘\W+’, test)
Output :
['Look',
'at',
'the',
'sky',
'We',
'are',
'not',
'alone',
'The',
'whole',
'universe',
'is',
'friendly',
'to',
'us',
'and',
'conspires',
'only',
'to',
'give',
'the',
'best',
'to',
'those',
'who',
'dream',
'and',
'work']
5. Findall() :
This method searches a string from left to right for all non-overlapping matches of the patter.
Example :
test = “Look at the sky…….. We are not alone. The whole universe is>>>>>>>friendly to us and conspires*only to give the best to those who dream and work”re.findall(‘\S+’, test)
Output :
['Look',
'at',
'the',
'sky........',
'We',
'are',
'not',
'alone.',
'The',
'whole',
'universe',
'is>>>>>>>friendly',
'to',
'us',
'and',
'conspires*only',
'to',
'give',
'the',
'best',
'to',
'those',
'who',
'dream',
'and',
'work']
6. Finditer() :
This method returns all the matches that were found from the given string.
Example :
import resentence = “Abdul Kalam was an Indian aerospace scientist who served as the 11th President of India from 2002 to 2007. He was born in 1931”
patterns = re.finditer(r”\d{4}”, sentence)for pattern in patterns:
print(pattern)
print(pattern.group())
Output :
<re.Match object; span=(93, 97), match='2002'>
2002<re.Match object; span=(101, 105), match='2007'>
2007<re.Match object; span=(122, 126), match='1931'>
1931
7. Sub() :
This function is used to replace a substring with another substring.
In the below example, the spaces are replaced with commas.
Example :
import restring = “V I B G Y O R”
match = re.sub(“\s”,”,”,string)
print (match)
Output :
V,I,B,G,Y,O,R
8. Escape() :
This method automatically escapes all the metacharacters from a string.
Example :
import reprint(re.escape(“Hello @ World”))
Output :
Hello\ @\ World
Why Regex is useful :
- Identifying the interested textual patterns.
- Removing the numbers or punctuations from the string.
- Identifying white spaces.
Use Cases of Regular Expression :
- Searching for the files on your computer.
- Document Scraping.
- In searching URL for substrings.
Conclusion :
This article is about different types of RegEx Functions with examples and use cases. I hope you guys enjoyed learning it.