A RegEx, or Regular Expression is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions. Import the re module: When you have imported the re module, you can start using regular expressions: For Example:
import re
#Check if the string starts with "The" and ends with "Spain":
TXT = "The rain in Spain"
x = re.search("The rain", TXT )
if (x):
print("YES! We have a match!")
else:
print("No match")
Output:
Yes! We have a match
Regular Expression Functions findall() The findall() function returns a list containing all matches. For Example :
import re str = "The rain in Spain" #Return a list containing every occurrence of "ai":
x = re.findall("ai", str)
print(x)
Output
[‘ai’, ‘ai’]
search()
The search() function searches the string for a match, and returns a match object if there is a match. If there is more than one match, only the first occurrence of the match will be returned: For Example :
import re TXT = "The rain in Spain" x = re.search('world', txt)
if x: print("pattern found inside the string") else: print("pattern not found") Output : pattern not found split()
The split() function returns a list where the string has been split at each match
For Example :
import re TXT = "The rain in Spain" x = re.split("\s", TXT) print(x) Output: ['The', 'rain', 'in', 'Spain'] The sub() function replaces the matches with the text of your choice
For Example : import re str = "The rain in Spain" x = re.sub("\s", ",", str) print(x) Output: The,rain,in,Spain
|