Quick Introduction to Python RegEx

In this article we will learn about Python RegEx

Python RegEx – Before moving ahead let’s know a little bit about Python JSON

RegEx – RegEx (Regular Expression), It is a sequence of character that finds a way of the searching pattern. it can also be used to check if a specified string is present in the given string or not.

RegEx Module – To use RegEx, import Python’s built-in module called re.

Import the re module.

import re

Example 1- Search if the string contains the specified element and ends with specified element.

import re

x = 'Hello! How are you doing?'
z = re.search('^Hello,*doing? $', x)
 
if z:
    print('Match found')
else:
    print('No match found') 
python-regEx

Explanation – As shown clearly, a match was not found; therefore, it returned else statement.

Example 2 – Search if the string contains the specified element and ends with specified element.

import re

x = 'Hello! How are you doing?'
z = re.search("are", x)

if z:
    print('Match found')
else:
    print('No match found') 
python-regEx

Explanation – As shown clearly, a match was found; therefore, it returned if statement.

RegEx Functions – The re module contains a set of functions that allows searching a string for a match.

This set of function follows as –

1. findall() function – It returns a list containing specified matches.

Example 1- It returns a list of every occurrence of specified match.

import re

x = 'Hello! How are you doing?'
z = re.findall('Hello How are you doing?', x)

print(z) 
python-regEx

Explanation – As shown clearly, a match was found; therefore, it returned string in the list.

Note: If it doesn’t contain specified match, then it returns an empty list.

Example 2- It returns an empty list.

import re

x = 'Hello! How are you doing?'
z = re.findall('Good', x)
print(z) 
python-regEx

Explanation – As shown clearly, a match was not found; therefore, it returned an empty list.

2. search() function – It searches the string for a match and returns the match object if there is a match.

Note: In case there is more than one match, only the first occurrence of the match is returned.

Example 1- It returns the match object (index number) of specified string.

import re

x = "Hello, Alex"
z = re.search('ex',  x) 

print(z) 
python-regEx

Explanation – As shown clearly, a match was found; therefore, it returned a match string with its index number (start and end).

Note: If no match is found, the value None is returned.

Example 2- It returns None.

import re

x = "Hello, Alex"
z = re.search('Gh' , x)

print(z)
python-regEx

Explanation – As shown clearly, a match was not found; therefore, it returned None.

3. split() function – It returns the list of spilt string at each match.

Example 1- Split the string at each match.

import re

x = 'Hello! Jello'
z = re.split('lo' , x)

print(z)
python-regEx

Explanation – As shown clearly, a match was found; therefore, it returned split the string.

Note: By specifying maxsplit parameter, number of occurrences can be controlled.

Example 2- Split the string at first occurrence of specified string.

import re

x = 'Hello! Jello, Sello'
z = re.split('lo' , x , 1)

print(z)
python-regEx

Explanation – As shown clearly, a match was found; therefore, it returned split character only with the first occurrence of a character.

Example 3- Check if string can be split or not.

import re

x = 'Hello! Jello'
z = re.split('&' ,  x) 

print(z)
python-regEx

Explanation – As shown clearly, a match was not found; therefore, it returned the original string.

4. sub() function – It replaces all matches with the specified text.

Example 1- It replaces all matches with the string ‘li’.

import re

x = 'Hello! Jello, Sello'
z = re.sub('lo' , 'li' , x)

print(z)
python-regEx

Explanation – As shown clearly, a match was found; therefore, it returned replaced string.

Note: Use of count parameter to control the number of occurrences.

Example 2- It replaces first two matches with the string ‘li’.

import re

x = 'Hello! Jello, Sello'
z = re.sub('lo' , 'li' , x , 2)

print(z)
python-regEx

Explanation – As shown clearly, a match was found; therefore, it returned replaced the first two specified strings.

Example 3 – Check if it replaces all matches with the string or not.

import re

x = 'Hello! Jello, Sello'
z = re.sub('Lo', 'li', x)

print(z)
python-regEx

Explanation – As shown clearly, a match was not found; therefore, it returned original strings.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Leave a Comment

Stay in the loop

codingstreets