Regular Expressions in Python

Want to know about regular expressions in Python? In this python tutorial we will discuss:

  • Regular Expressions in Python
  • Functions of regular expressions in Python
  • Python regular expressions special sequence
  • Python regular expression functions
  • search function()
  • split function()
  • findall function()
  • sub function()
  • Python Validation Examples (using regular expressions)
  • Python Password field validation
  • Mobile number field validation in Python
  • Email field validation in Python
  • Python Zip-code field validation
  • URL field validation in Python

Regular Expressions in Python

Regular expression or also known as regex patterns helps us to find or match using various patterns. In Python, we can use regular expressions by importing re module like below:

To import re module in Python, you can use the below system.

import re

Functions of regular expressions in Python

The re module has some functions that you can use:

  • search(): This function returns the match object from the string
  • split(): This function splits a given string into lists
  • find all(): This function returns all the matches from the string
  • sub(): This function replaces any match in the string

Python regular expressions special sequence

Here are a few Python regular expressions special sequences that are characters based on their functionality.

Special sequence
\A -This returns a match of specified characters that are at the beginning of the string.
\Z – This returns a match of specified characters that are at the end of the string.
\b – This returns a match of specified characters that are at the beginning or end of the string.
\B – This returns a match of specified character which are not present at the beginning or end of the string.
\d – This returns a match if the digit is present in the string.
\D – This returns a match if the digit is not in the string.
\s – This returns a match when the character contains whitespace in it.
\S – This returns a match when the character does not contain whitespace in it.
\w – This returns a match when a string contains words characters in it.
\W – This returns a match when a string does not contain words characters in it.

Python regular expression functions

Now, let us check out a few Python functions that you can use with Python regular expressions.

search function()

Now we can see how to perform search function in regular expression in python.

In this example, I have imported a module re and assigned a string as “Mango is yellow” re.search()function is used here to return the match object from the string.

‘\Amango’ returns characters which are at the beginning. To return the character which is at the end ‘\zstring’ is used.

Example:

import re
string = "Mango is yellow"
match = re.search('\AMango', string)
if match:
  print("pattern found")
else:
  print("pattern not found")  

Below image shows the output:

Python search function()
Python search function()

split function()

Now we can see how to perform split function() of regular expression in python

In this example, I have imported a repackage,re.split(pattern, a) this function split the string where there is a match and returns a list of strings where splits occurred.

Example:

import re
a = 'mango 4 is yellow.'
pattern = '\d'
res = re.split(pattern, a) 
print(res)

Below image shows the output:

Python split function()
Python split function()

findall function()

Now we can see how to perform findall () of regular expression in python

In this example, I have taken a string with both character and also digits, re.findall() function finds all the matches and returns a list of matches as a string ‘\d+’ This returns digits from the string.

Example:

import re
a = 'mango 24 is 12 yellow 6'
pattern = '\d+'
res = re.findall(pattern, a) 
print(res)

Below image shows the output:

Python findall function()
findall function()

sub function()

Now we can see how to perform sub function() of regular expression in python

In this example, I have replaced whitespace between the strings, re.sub() function replaces the occurrence of a particular substring with another substring.

Example:

import re
string = 'I am having silver color car'

pattern = '\s'
replace = ''
newstring = re.sub(pattern, replace, string) 
print(newstring)

Below image shows the output:

Python sub function()
sub function()

Read: Python NumPy concatenate and Python NumPy read CSV

Python Validation Examples (using regular expressions)

Let us check out a few Python validation examples using Python regular expressions.

Python Password field validation

Now, let us see how to a password field validation in Python.

Validation:

  • At least a letter between[a-z]
  • At least a letter between[A-Z]
  • At least a number between[0-9]
  • At least a character from[$#@]

Now in this concept we can see how to validate password using regular expression in python.

In this example, I have taken the length of the password should be greater than 6 or less than 11 and the password should contain a letter between[a-z], a letter between[A-Z], a number between[0-9], and character from[$#@], if the condition is valid it should print “Valid Password” else ” Not a Valid Password”.

Example:

import re
a= input("enter your password")
x = True
while x:  
    if (len(a)<6 or len(a)>11):
        break
    elif not re.search("[a-z]",a):
        break
    elif not re.search("[0-9]",a):
        break
    elif not re.search("[A-Z]",a):
        break
    elif not re.search("[$#@]",a):
        break
    elif re.search("\s",a):
        break
    else:
        print("Valid Password")
        x=False
        break

if x:
    print("Not a Valid Password")

Below image shows the output:

Python Password field validation
Password field validation

Mobile number field validation in Python

Now we can see how to validate mobile number using regular expression in python.

  • The starting digit should be either (0/91)
  • The first digit should be between [7-9]
  • The next 9 digits should be between [0-9]

In this example, I have taken the pattern “9945555555” as a mobile number, and validation as

("(0/91)?[7-9][0-9]{9}")

Example:

import re 
def isValid(p): 
    Pattern = re.compile("(0/91)?[7-9][0-9]{9}") 
    return Pattern.match(p) 
p = "9945555555"
if (isValid(p)):  
    print ("Valid Number")      
else : 
    print ("Invalid Number") 

This way you can validate mobile number field validation in Python.

Read: Python NumPy log

Email field validation in Python

Now we can see how to validate an email using regular expression in python

In this example ,I have an input string and validation format is [A-Za-z0-9-_]+(.[A-Za-z0-9-_]+)*@[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,}). IGNORECASE allows case sensitive matching in regular experssion. re.match() will search the regular expression pattern and returns the list of the string.

Example:

import re
email = input ("Please type in an email address ")
if re.match("\A(?P<name>[\w\-_]+)@(?P<domain>[\w\-_]+).(?P<toplevel>[\w]+)\Z",email,re.IGNORECASE):
    print("email is valid")
else:
    print("email is invalid")

The detailed format of validation

[A-Za-z0-9-_]+(.[A-Za-z0-9-_]+)*@[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})

Below image shows the output:

Check the validation of Email
Email field validation

Python Zip-code field validation

Now we can see how to validate zipcode using regular expression in python

In this example, we can see how to validate a zip code, re.match() will search the regular expression pattern and returns the list of the string, re.findall() searches all the patterns in the string.

  • \d: Match and capture a digit in the list
  • ?=: The preceding term is optional and matched almost once
  • \1: Back-reference to captured group
  • <2: The end digit should be lesser than true

Example:

import re
a=input('enter a zipcode')
b = (re.match(r'^[1-9][\d]{5}$',a) and len(re.findall(r'(\d)(?=\d\1)',a))<2 )
print(b)

Below image shows the output for Python zip code validation.

check the validity of zip code:
Zip-code field validation

URL field validation in Python

Here we can see how to validate URL using regular expression in python

In this example, I have taken validation format as the url must start with(http or https), next it should have ://and www. and then length{2,150}, and lastly domain name.com,.org

Example:

import re
def isValidURL(string):
    regex = ("((http|https)://)(www.)?" +"[a-zA-Z0-9@:%._\\+~#?&//=]" +"{2,150}\\.[a-z]" +"{2,6}\\b([-a-zA-Z0-9@:%" +"._\\+~#?//&=]*)")
    a = re.compile(regex)
    if (string == None):
        return False
    if(re.search(a, string)):
        return True
    else:
        return False
url = "https://pythonguides.com"
 
if(isValidURL(url) == True):
    print("valid URL")
else:
    print("Not valid URL")

Below image shows the output:

Check the validation for URL
URL field validation

You may like the following Python tutorials:

I hope this tutorial helps to learn regular expressions in python, covers the below topics.

  • Regular Expressions in Python
  • Functions of regular expressions in Python
  • Python regular expressions special sequence
  • Python regular expression functions
  • search function()
  • split function()
  • findall function()
  • sub function()
  • Python Validation Examples (using regular expressions)
  • Python Password field validation
  • Mobile number field validation in Python
  • Email field validation in Python
  • Python Zip-code field validation
  • URL field validation in Python