We may earn a commission if you make a purchase through the links on our website.

Python RegEx Cheat Sheet

Python RegEx Cheat Sheet

Hitesh J UPDATED: April 4, 2023

Regular Expressions are patterns used to match character combinations in strings. They are handy in Python Programming Language for searching and even replacing the specified text pattern. The Python has a module called RE that provides full support for regular expressions in Python. In simple terms, A regex in Python is a unique sequence of characters that defines a pattern for complex string-matching functionality.

This Python RegEx cheat sheet will show you the most commonly used regular expressions that any network or system admin can use as a quick reference.

Basic Characters

Here, we will show you some basic characters that are used to perform simple exact matches.

Characters Explanation
a It Matches exactly one character a.
ab Matches the string ab.
a|b Matches a or b. If a is matched, b is left.
$ Match the end of the string.
i Ignore case.
s Matches everything, including newline as well.
u Matches Unicode character classes.
x Allow spaces and comments (Verbose).
^ Match the start of the string.
* Match 0 or more repetitions.
+ Match one or more times.
? Match zero or one time.
{a,b} Match a to b times.
{a,} Match at least a time.
{,b} Match up to b times.
{a} Match exactly a times.
{a,b}? Matches the expression to its left times, and ignores b.

Character Class

A character class defines a set of characters. It is the most basic regex concept after a literal match. It makes one small sequence of characters match a more extensive set of characters.

Class Explanation
\d Matches digits from 0-9.
\D Matches any non-digits.
\w Matches alphanumeric characters including, a-z, A-Z, 0-9, and underscore(_).
\W Matches any character, not a Unicode word character.
\s Matches whitespace characters.
\S Matches non-whitespace characters.
\n Matches a newline character.
\t Matches tab character.
\b Matches empty string, only at the beginning or end of a word.
\Z Matches the expression to its left at the absolute end of a string, whether in single or multi-line mode.
\A Matches the expression to its right at the absolute start of a string, whether in single or multi-line mode.

Character Sets

A character set is a set of characters put in square brackets. The Python regex matches any one out of several characters in the character set.

Sets Explanation
[a-z] Match any lowercase ASCII letter.
[xyz] Matches either x, y, or z.
[x\-z] Matches x, – or z.
[-x] Matches – or x.
[a-d0-9] Matches characters from a to d or from 0 to 9.
[^xy4] Matches characters that are not x, y, or 4.
[(+*)] Matches (, +, * or ).
[0-5][0-9] Matches for any two-digit numbers from 00 and 59.
[^ab5] Adding ^ excludes any character in the set. Here, it matches characters that are not a, b, or 5.

Regular Expression(RE) Module Functions

Python has a built-in module called re, used to work with Regular Expressions. The re module offers a set of functions that allows us to search a string for a match.

RE Functions Explanation
re.Match It will search the regular expression pattern and return the first occurrence.
re.search It takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.1
re.fullmatch It will return a match object if and only if the entire string matches the pattern. Otherwise, it will return None.
re.compile It can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search for a pattern again without rewriting it.
re.sub It is used to replace occurrences of a particular sub-string with another sub-string.
re.escape It will return a string with all non-alphanumerics backslashed. this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
re.split It will split a string by multiple delimiters.
re.findall It will return all non-overlapping matches of pattern in the string, as a list of strings.
re.subn It will return the new string along with the no. of replacements.

Python RegEx Examples

In this section, we will show you some real-life Python regex examples.

Find the type of the IP Address

Here, we will write a Python program that uses regex to check whether the given IP address is IPv4, IPv6 or none.

nano checkip.py

Add the following code:
import re
ipv4 = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$'''
ipv6 = '''(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|
([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:)
{1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1
,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}
:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{
1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA
-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a
-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0
-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,
4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}
:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9
])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0
-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]
|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]
|1{0,1}[0-9]){0,1}[0-9]))'''
def find(Ip):
if re.search(ipv4, Ip):
print("IPv4")
elif re.search(ipv6, Ip):
print("IPv6")
else:
print("None")
# Driver Code
if __name__ == '__main__' :
# Enter the Ip address
Ip = "192.168.0.100"
find(Ip)

Save and close the file, then run the program with the following command:

python3 checkip.py

If the IP address (192.168.0.100) given in the above code is IPv4, then you should get the following output:

IPv4

Check the Given IPv4 Address is reserved or not

In this example, we will check whether the given IP address is reserved or not. Some IP addresses are reserved for experimental and research purposes. The IP address range 240.0.0.0 – 255.255.255.254 is reserved for research purposes.

Let's create a new file to check the reserved IP address:

nano checkreservedip.py

Add the following code:

from ipaddress import ip_address

def reservedIPAddress(IP: str) -> str:
return “Reserved” if (ip_address(IP).is_reserved) else “Not Reserved”

if __name__ == ‘__main__' :

# Not Reserved
print(reservedIPAddress(‘192.168.0.123'))

# Reserved
print(reservedIPAddress(‘240.0.0.20'))

Save and close the file, then run this using the following command:

python3 checkreservedip.py

You should get the following output:

Not Reserved
Reserved

This means the IP address 192.168.0.123 is not reserved, and the IP address 240.0.0.20 is reserved.

Test IP Address is Valid or Invalid

In this example, we will test whether the given IP address is valid or not.

First, create a test.py file:

nano test.py

Add the following code:

import re
k = 0
while k < 5 :
i = input("\nEnter Ip address : ")
ip = re.match("^([1][0-9][0-9].|^[2][5][0-5].|^[2][0-4][0-9].|^[1][0-9][0-9].|^[0-9][0-9].|^[0-9].)([1][0-9][0-9].|[2][5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([1][0-9][0-9].|[2][5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([1][0-9][0-9]|[2][5][0-5]|[2][0-4][0-9]|[1][0-9][0-9]|[0-9][0-9]|[0-9])$",i)
k = k + 1
if ip:
print ("\n=====================")
print ("Valid IP address")
print ("=====================")
break
else :
print ("\nInvalid IP")
else :
print ("\nAllowed Max 5 times")

Save and close the file, then run this script with the following command:

python3 test.py

You will be asked to provide any IP address as shown below:

Enter Ip address: 192.168.0.111

Provide any IP address and hit Enter. You will get the following output:

=====================
Valid IP address
=====================

Now, return this script and provide an invalid IP address. You will get the following result:

Enter Ip address: 10.123.342.255

Invalid IP

Check a Valid Email Address

In this example, we will check whether the given Email address is valid or not.

Let's create a test.py file using the following command:

nano test.py

Add the following code:

import re
input_string = input("Enter Email address : ")
regex_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'

result = bool( re.match( regex_pattern, input_string))
if (result):
print (“Valid Email address”)
else:
print (“Invalid Email address”)

Save and close the file, then run this with the following command:

python3 test.py

You will be asked to provide an Email address as shown below:

Enter Email address : hitjethva@gmail.com

Provide any email address and hit Enter. If the given Email address is valid, you should get the following result.

Valid Email address

Print Hostname and IP address of System

In this example, we will print the IP address and hostname of your system.

Let's create a test.py file with the following command:

nano test.py

Add the following code:

import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your System Hostname is:" + hostname)
print("Your System IP Address is:" + IPAddr)

Save and close the file then run this script with the following command:

python3 test.py

You should get the following output:

Your System Hostname is:newpc

Your System IP Address is:127.0.0.1

Scan Open Port on Remote Host

In this example, we will scan a remote host and print all open ports.

Let's create a test.py python script with the following command:

nano test.py

Add the following code:

from socket import *
import time
startTime = time.time()

if __name__ == ‘__main__':
target = input(‘Enter the host to be scanned: ‘)
t_IP = gethostbyname(target)
print (‘Starting scan on host: ‘, t_IP)

for i in range(50, 500):
s = socket(AF_INET, SOCK_STREAM)

conn = s.connect_ex((t_IP, i))
if(conn == 0) :
print (‘Port %d: OPEN' % (i,))
s.close()
print(‘Time taken:', time.time() – startTime)

Save and close the file then run the above script with the following command:

python3 test.py

You will be asked to provide an IP address of the remote host as shown below:

Enter the host to be scanned: 172.20.10.3

Provide an IP address and hit Enter. You will get the following result:

Starting scan on host: 172.20.10.3
Port 80: OPEN
Port 111: OPEN
Port 139: OPEN
Port 445: OPEN
Time taken: 1.1808812618255615

Conclusion

I have provided a list of all the most commonly used Python RegEx with real-life examples in the above cheat sheet. I hope you can use it as a quick reference.

Python RegEx Cheat Sheet FAQs

What are some common uses of regex?

  • Validating input fields (such as email addresses or phone numbers)
  • Searching and replacing text
  • Extracting specific information from text (such as dates or URLs)

What are some common regex metacharacters and what do they do?

  • '.' (dot): matches any single character
  • '*' (asterisk): matches zero or more of the preceding character or group
  • '+' (plus): matches one or more of the preceding character or group
  • '?' (question mark): matches zero or one of the preceding character or group
  • '|' (vertical bar): matches either the expression before or after the bar
  • '^' (caret): matches the beginning of a string
  • '$' (dollar sign): matches the end of a string

What are some common regex quantifiers and what do they do?

  • {n}: matches exactly n occurrences of the preceding character or group
  • {n,}: matches at least n occurrences of the preceding character or group
  • {n,m}: matches at least n and at most m occurrences of the preceding character or group

What are some common regex character classes and what do they do?

  • [abc]: matches any single character from the set a, b, or c
  • \d: matches any single digit (0-9)
  • \w: matches any word character (letters, digits, or underscores)
  • \s: matches any whitespace character (spaces, tabs, or line breaks)