In this article, We are going to cover Python RegEx with Examples, Compile Method in Python, findall() Function in Python, The split() function in Python, The sub() function in python.
A Regular Expression is a sequence of characters that defines a search pattern.When we import re module, you can start using regular expressions.
Example of Python RegEx:
Search the string from start with and end with :
import re
a = "Regular Expression is a sequence of characters"
x = re.search("^Regular.*characters$", a)
if x:
print("Match")
else:
print("No match")
Output:
Match
Compile Method in Python:
The purpose of the Compile Method is to compile the regex pattern which will be used for matching later. And Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern of string.
Example of Compile method in python:
import re
# it will match string l to w
p = re.compile('[l-w]')
print(p.findall("Welcome to Python programming"))
Output:
['l', 'o', 'm', 't', 'o', 't', 'o', 'n', 'p', 'r', 'o', 'r', 'm', 'm', 'n']
Another Example of compile method of python (numbers example):
import re
# it will match numbers 0 to 9
p = re.compile('[0-7]')
print(p.findall("Welcome 21 to Python 4 programming 7 9"))
Output:
['2', '1', '4', '7']
findall() Function in Python:
The findall() function returns a list of containing all matches.
Example of findall() function in Python:
import re
text = "A sentence word is a single word that forms a full sentence"
x = re.findall("word", text)
print(x)
Output:
['word', 'word']
If in this list no matches are found, an empty list is returned.
Example of findall() function in python:
import re
text = "A sentence word is a single word that forms a full sentence"
x = re.findall("to", text)
print(x)
if (x):
print("Match")
else:
print("No match")
Output:
[]
No match
Example of findall() function in python:
import re
text = "Hello my I'm 21 years old and my friend is 26 years old"
x = re.findall('\d+',text)
print(x)
Output:
['21', '26']
The split() function in Python:
In python split() this is the most efficient and commonly used method to split on multiple characters at once.
Example of split() function in Python:
import re
text = "welcome to python programming"
x = re.split("\s", text)
print(x)
Output:
['welcome', 'to', 'python', 'programming']
The sub() function in python:
This sub() function replaces the matches with the text of your choice.
Example of sub() function in Python:
import re
text = "Welcome to python programming"
x = re.sub("\s", "2", text)
print(x)
Output:
Welcome2to2python2programming
Conclusion:
In this article We have covered Python RegEx with Examples, Compile Method in Python, findall() Function in Python, The split() function in Python, The sub() function in python.
Related Articles:
Functions in Python with Examples
Loops in Python 3 with Examples
7 Python Operators with Examples
Python Introduction for Programmers [Part 1]
6 Python Conditional Statements with Examples
Inheritance in python with examples
Encapsulation and Polymorphism in Python with Examples
Reference: