Regular Expression in Python with Examples?

Regular expressions is a kind of programming language which is used to identify whether a pattern exists in a given sequence of characters (string) or not.
Regular expression or Regex is a sequence of characters that is used to check if a string contains the specified search pattern.
To use RegEx module, python comes with built-in package called re, which we need to work with Regular expression. To use RegEx module, just import re module.
import re
import re
txt = "Use of python in Machine Learning"
x = re.search("^Use.*Learning$", txt)
if (x):
   print("YES! We have a match!")
else:
   print("No match")
YES! We have a match!
The re module offers couples of functions that allows us to search a string for a match.
Metacharacters in RegEx are characters with a special meaning.
Special sequences in RegEx is a \ followed by one of the characters listed below and has a special meaning -
A set in RegEx is a set of characters inside a pair of square brackets [] having some special meaning.
 
The findall() function returns a list containg all matches.
#Print a list of all matches (“in”) from a text
import re
txt = "Use of python in Machine Learning"
x = re.findall("in", txt)
print(x)
['in', 'in', 'in']
Above output display list contains all the matches in the order they are found. However, if no match found, an empty list is displayed.
Just change the below line in your above program, “pattern” which is not there in the text or string.
x = re.findall("Hello", txt)
[]
The search() function searches the string and returns a match object if match is found.
However, if there are more than one match, only the first occurrence of the match will be returned.
import re
txt = "Python is one of the most popular languages around the world"
searchObj = re.search("\s", txt)
print("The first white-space character is located in position: ", searchObj.start())
The first white-space character is located in position: 6
However, if no match found then None is returned.
The split() function in RegEx returns a list where the string has been split at each match -
# Split at each white-space character
import re
string = "Python is one of the most popular languages around the world"
searchObj = re.split("\s", string)
print(searchObj)
['Python', 'is', 'one', 'of', 'the', 'most', 'popular', 'languages', 'around', 'the', 'world']
The sub() function in RegEx is to replace the match with the text of your choice.
#Replace every white-space in the string with _:
import re
string = "Python is one of the most popular language around the world"
searchObj = re.sub("\s", "_", string)
print(searchObj)
Python_is_one_of_the_most_popular_language_around_the_world
A match object in RegEx is an object containing information about the search and the result. In no match found, None is returned.
Example - Search a string and returned match object.
import re
string = "Python is one of the most popular language around the world"
searchObj = re.search("on", string)
print(searchObj)
<_sre.SRE_Match object; span=(4, 6), match='on'>
The match object has properties and methods used to retrieve information about the search, and the Result.
.span() – returns a tuple containing the start and end position of the match found.
.span() – returns a tuple containing the start and end position of the match found.
.string – returns the string passed into the function.
.string – returns the string passed into the function.
.group() – returns the part of the string where there was a match.
.group() – returns the part of the string where there was a match.
Example - Print the part of the string where there was a match.
#Looks for any words that starts with the an upper case “P”:
import re
string = "Python is one of the most popular language around the world"
searchObj = re.search(r"\bP\w+", string)
print(searchObj)
<_sre.SRE_Match object; span=(0, 6), match='Python'>