Regular Expression In Python | FACE Prep

The Regular Expression, also known as RegEx in Python is a sequence of metacharacters, numbers and alphabets that define a particular pattern (email, password, username pattern). Before understanding its working, let us discuss how they are useful in programming.

Why Do We Need RegEx

The regular expression in Python is used to identify whether a pattern exists in the given string or not. For example, consider yourself as a Manager and you have a list of all your employee names. You need to send a common message to the employees whose name starts with the alphabet ‘h’, ends with the alphabet ‘i’ and of length 6. The below-given RegEx helps you to do this.

^h....i$

The pattern is, any six-letter string starting with the alphabet ‘h’ and ending with ‘i’. Here, the symbols ‘^’ and ‘$’ are called metacharacters. Below is the matching status of various names.

Pattern

Names

Status

^h….i$

harini

Matched

^h….i$

harsit

Not Matched

^h….i$

hrtyei

Matched

Next, let us see how to implement this RegEx in Python programs.

How To Implement RegEx In Python 

To check whether the given string matches with the given RegEx pattern, we need to use a method named ‘match()’. This method is pre-defined in the module named ‘re’. In the below example, the string ‘harini’ is compared with the RegEx pattern ‘^h….i$’.

from re import *
pattern = '^h....i$' #RegEx pattern
str1 = 'harini' #string to be matched with RegEx pattern
res = match(pattern, str1) #result of the match
if(res):
    print("Pattern Matched") #gets printed if the result of the match is true
else:
    print("Pattern Not Matched") #gets printed if the result of the match is false
Output:
Pattern Matched

Let us discuss more metacharacters and their uses.

RegEx Metacharacters 

Some of the frequently used metacharacters are listed below. 

Metacharacter

Name

Description

[]

Square Brackets

Checks if a string contains any one of the characters given within it

.

Period

Matches any single character except the newline ‘\n’

^

Caret

Checks if a string starts with the specified character or not

$

Dollar

Checks if a string ends with the specified character or not

*

Star

Checks for zero or more characters to its left

+

Plus

Checks for one or more characters to its left

?

Question Mark

Checks for exactly zero or one character to its left

RegEx Special Sequences

Some of the frequently used special sequences are listed below. 

Special Sequence

Name

Description

\A

Uppercase A

Matches if the specified characters are at the start of a string

\d

Lowercase d

Matches only numbers from 0 to 9

\D

Uppercase D

Matches other than numbers

\w

Lowercase w

Matches any single alphabet, number, underscore

\Z

Uppercase Z

Matches if the specified characters are at the end of a string

regular expression in python
‘re’ module provides some more methods to work with RegEx. These methods are used to do the operations of finding, replacing and formatting the data using RegEx. Let us discuss them with examples.

Regular Expression In Python Examples

a) findall()

This method returns a list of strings containing all matches. In the below example, ‘findall’ method checks if either of the characters ‘A’ or ‘F’ or ‘P’ is present in the string “FACE Prep”. If so, then prints them in the form of a list.

from re import *
res = findall('[AFP]', 'FACE Prep')
print(res)
Output:
['F', 'A', 'P']

b) split()

This method splits the string where there is a match and returns the result in the form of a list. In the below example, ‘split’ method splits the string “FACE10Prep3Python” whenever it comes across a number.

from re import *
res = split('\d+', 'FACE10Prep3Python')
print(res)
Output:
['FACE', 'Prep', 'Python']

c) search()

This method is used to search for the given pattern in the given string. In the below example, the ‘search’ method checks whether the characters ‘FA’ is at the start of the string “FACE Prep”.

from re import *
res = search('\AFA', 'FACE Prep')
if(res):
    print("Found")
else:
    print("Not Found")
Output:
Found

Regular Expression In Python FAQs

The Regular Expression in Python is a sequence of characters that defines a particular search pattern. It is used to identify whether a pattern exists in the given string or not.

RegEx pattern: m* and m+

Example code 1:
from re import *
res = search('m*', 'Programming')
if(res):
    print("Found")
else:
    print("Not Found")


Example code 2:
from re import *
res = search('m+', 'Programming')
if(res):
    print("Found")
else:
    print("Not Found")

RegEx pattern: !?

Example code:
from re import *
res = search('!?', 'Hello World!')
if(res):
    print("Found")
else:
    print("Not Found")