Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Regex split()
Next article icon

Regex Cheat Sheet – Python

Last Updated : 14 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Regex or Regular Expressions are an important part of Python Programming or any other Programming Language. It is used for searching and even replacing the specified text pattern. In the regular expression, a set of characters together form the search pattern. It is also known as the reg-ex pattern. The tough thing about Regex is not learning or understanding it but remembering syntax and how to form patterns according to our requirements.

So, here we have provided a Regex Cheat Sheet containing all the different character classes, special characters, modifiers, sets, etc. which are used in the regular expression.

Table of Content

  • Basic Characters
  • Quantifiers
  • Character Classes
  • Sets
  • Groups
  • Assertions
  • Flags

Basic Characters:

Expression 

Explanations

^

Matches the expression to its right, at the start of a string before it experiences a line break

$

Matches the expression to its left, at the end of a string before it experiences a line break

.

Matches any character except newline

a

Matches exactly one character a

xy

Matches the string xy

a|b

Matches expression a or b. If a is matched first, b is left untried.

Example:

Python3




import re
  
print(re.search(r"^x","xenon"))
print(re.search(r"s$","geeks"))
 
 

Output:

<re.Match object; span=(0, 1), match='x'>
<re.Match object; span=(4, 5), match='s'>

Explanation:

First import the regex module with the command import re

Then, in the first example, we are searching for “^x” in the word “xenon” using regex. ^ this character matches the expression to its right, at the start of a string. So, ^x will search for character x in the beginning of the string. Since xenon starts with x, it will find the match and will return the match(‘x’) and its position(0,1)

Similarly, in the second example s$ will search for the character s at the end of the string, now since geeks end with s, so it will find the match and will return the match(‘s’) and its position(4,5).

Quantifiers:

Expressions Explanations

+

Matches the expression to its left 1 or more times.

*

Matches the expression to its left 0 or more times.

?

Matches the expression to its left 0 or 1 times

{p}

Matches the expression to its left p times, and not less.

{p, q}

Matches the expression to its left p to q times, and not less.

{p, }

Matches the expression to its left p or more times.

{ , q}

Matches the expression to its left up to q times

Their default searching method is Greedy. But if ? is added to qualifiers (+, *, and ? itself) it will perform matches in a non-greedy manner.

Examples:

Python3




import re
  
print(re.search(r"9+","289908"))
print(re.search(r"\d{3}","hello1234"))
 
 

Output:

<re.Match object; span=(2, 4), match='99'>
<re.Match object; span=(5, 8), match='123'>

Explanation:

In the first example, 9+ will search for the number 9 one or more times. Since 289908 contains 9 two times, the regex will match it and will print the match(’99’) and its position(2,4)

In the second example, \d{3} will search for digits exactly 3 times. Since hello1234 have digits, it will match exactly 3 digits which will be encountered first that are 123 and not 4, because {3} will match for exactly 3 digits. So it will print the match(‘123’) and its position(5,8)

Character Classes:

Expressions Explanations

\w 

Matches alphanumeric characters, that is a-z, A-Z, 0-9, and underscore(_)

\W

Matches non-alphanumeric characters, that is except a-z, A-Z, 0-9 and _

\d

Matches digits, from 0-9.

\D 

Matches any non-digits.

\s

Matches whitespace characters, which also include the \t, \n, \r, and space characters.

\S

Matches non-whitespace characters.

\A

Matches the expression to its right at the absolute start of a string whether in single or multi-line mode.

\Z 

Matches the expression to its left at the absolute end of a string whether in single or multi-line mode.

\n

Matches a newline character

\t

Matches tab character

\b

Matches the word boundary (or empty string) at the start and end of a word.

\B

Matches where \b does not, that is, non-word boundary

Examples:

Python3




import re
  
print(re.search(r"\s","xenon is a gas"))
print(re.search(r"\D+\d*","123geeks123"))
 
 

Output:

<re.Match object; span=(5, 6), match=' '>
<re.Match object; span=(3, 11), match='geeks123'>

Explanation:

In the first example, \s will search for the blank space and whenever it encounters the first blank space it will print out that match. Since xenon is a gas that contains blank spaces, it will encounter the first blank space and print out that match(‘ ‘) and its position(5,6)

In the second example, \D+\d* will search for one or more non-digits characters followed by 0 or more digits. In our case, geeks123 best fits the description as it contains 1 or more non-digits characters(geeks) followed by 0 or more digit characters(123). So it will print the match(‘geeks123’) and its position(3,11).

Sets:

Expressions Explanations

[abc]

 Matches either a, b, or c. It does not match abc.

[a-z]

Matches any alphabet from a to z.

[A-Z]

Matches any alphabets in capital from A to Z

[a\-p]

Matches a, -, or p. It matches – because \ escapes it.

[-z]

Matches – or z

[a-z0-9]

Matches characters from a to z or from 0 to 9.

[(+*)] 

Special characters become literal inside a set, so this matches (, +, *, or )

[^ab5] 

Adding ^ excludes any character in the set. Here, it matches characters that are not a, b, or 5.

\[a\]

Matches [a] because both parentheses [ ] are escaped 

Examples:

Python3




import re
  
print(re.search(r"[^abc]","abcde"))
print(re.search(r"[a-p]","xenon"))
 
 

Output:

<re.Match object; span=(3, 4), match='d'>
<re.Match object; span=(1, 2), match='e'>

Explanation:

In the first example, [^abc] will search for anything except a, b and c, so regex will match the first character which is not a or b or c and will print out that match. Since abcde contains d as its first match that is neither a nor b nor c, it will print out that match. So match would be (‘d’) and its position would be (3,4)

In the second example, [a-p] will search for the character between a to p. In xenon the first word between a-p is e it will print out that search. So match would be (‘e’) and its position would be (1,2)

Groups:

Expressions Explanations

( )

Matches the expression inside the parentheses and groups it which we can capture as required

(?#…)

Read a comment

(?PAB)

Matches the expression AB, which can be retrieved with the group name.

(?:A)

Matches the expression as represented by A, but cannot be retrieved afterwards.

(?P=group)

 Matches the expression matched by an earlier group named “group”

Examples:

Python3




import re
  
example = (re.search(r"(?:AB)","ACABC"))
print(example)
print(example.groups())
  
result = re.search(r"(\w*), (\w*)","geeks, best")
print(result.groups())
 
 

Output:

<re.Match object; span=(2, 4), match='AB'>
()
('geeks', 'best')

Explanation:

In the first example, (?:AB) will search and matches the expression AB and will print out the match and its position. Since ACABC contains AB, it will print the match(‘AB’) and its position(2,4) but as stated, this cannot be retrieved afterward. So, if we try to print the group of the output, it will show an empty bracket.

In the second example, we have captured two groups, one group which has 0 or more alphanumeric characters followed by comma and space and then followed by another group which has again 0 or more alphanumeric characters. In geeks, best geeks and best are captured as the first and second groups. So, when we print out these groups we will have (‘geeks’, ‘best) as the groups captured. 

Assertions:

Expression Explanation

A(?=B)

This matches the expression A only if it is followed by B. (Positive look ahead assertion)

A(?!B)

This matches the expression A only if it is not followed by B. (Negative look ahead assertion)

(?<=B)A

This matches the expression A only if B is immediate to its left.  (Positive look behind assertion)

(?<!B)A 

This matches the expression A only if B is not immediately to its left. (Negative look behind assertion)

(?()|)

If else conditional

Examples:

Python3




import re
  
print(re.search(r"z(?=a)", "pizza"))
print(re.search(r"z(?!a)", "pizza"))
 
 

Output:

<re.Match object; span=(3, 4), match='z'>
<re.Match object; span=(2, 3), match='z'>

Explanation:

In the first example, z(?=a) will search for the character z which is followed by the character a. Since in pizza, we have a character z which is immediately followed by the character a (pizza), so there will be a match. Regex will print the match(‘z’) which is followed by a with its position(3,4)

In the second example, z(?!a) will search for the character z which is not followed by the character a. Since in pizza, we have a character z which is not followed by a but followed by z (pizza), so there will be a match. Regex will print the match(‘z’) not followed by a with its position (2,3)

Flags:

Expression Explanation

a

Matches ASCII only

i 

Ignore case

L

Locale character classes

m

^ and $ match start and end of the line (Multi-line)

s

Matches everything including newline as well

u

 Matches Unicode character classes

x

Allow spaces and comments (Verbose)

Examples:

Python3




import re
  
exp = """hello there
I am from
Geeks for Geeks"""
  
print(re.search(r"and", "Sun And Moon", flags=re.IGNORECASE))
print(re.findall(r"^\w", exp, flags = re.MULTILINE))
 
 

Output:

<re.Match object; span=(4, 7), match='And'>
['h', 'I', 'G']

Explanation:

In the first example, the IGNORECASE flag will search for the word and irrespective of its case (whether uppercase or lowercase), So it ignores the case and matches And in the expression. So it will print the match(‘And’) and its position(4,7)

In the second example, the MULTILINE flag will search in each and every line and will match when the line is starting an alphanumeric character. Since in Multi-line hello there I am from Geeks for Geeks, every line is starting with an alphanumeric character, so it will match every line and will print the match in an array ([‘h’, ‘I’, ‘G’]).

Note: In MULTILINE flag, we have to use re.findall, since it has many matches (for every line)

Conclusion

The Python Regex Cheat Sheet is a concise valuable reference guide for developers working with regular expressions in Python, which covers all the different character classes, special characters, modifiers, sets etc. which are used in the regular expression. It is a valuable resource for anyone who wants to learn how to use regex in Python.



Next Article
Python - Regex split()

I

img2018033
Improve
Article Tags :
  • Python
  • Technical Scripter
  • python-regex
  • Technical Scripter 2020
Practice Tags :
  • python

Similar Reads

  • Python - Regex split()
    re.split() method in Python is generally used to split a string by a specified pattern. Its working is similar to the standard split() function but adds more functionality. Let’s start with a simple example of re.split() method: [GFGTABS] Python import re s = "Geeks,for,Geeks" # Using re.s
    3 min read
  • Python RegEx
    In this tutorial, you'll learn about RegEx and understand various regular expressions. Regular ExpressionsWhy Regular ExpressionsBasic Regular ExpressionsMore Regular ExpressionsCompiled Regular Expressions A RegEx is a powerful tool for matching text, based on a pre-defined pattern. It can detect t
    9 min read
  • How to Import Regex in Python
    Regex is a built-in library in Python. You can use the re module to work with regular expressions. Here are some basic examples of how to use the re module in Python: Examples of Regular Expression in PythonWe can import it in our Python by writing the following import statement in the Python script
    2 min read
  • Python Regex: Replace Captured Groups
    Regular Expressions, often abbreviated as Regex, are sequences of characters that form search patterns. They are powerful tools used in programming and text processing to search, match, and manipulate strings. Think of them as advanced search filters that allow us to find specific patterns within a
    5 min read
  • Python Match Case Statement
    Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement
    8 min read
  • re.search() in Python
    re.search() method in Python helps to find patterns in strings. It scans through the entire string and returns the first match it finds. This method is part of Python's re-module, which allows us to work with regular expressions (regex) simply. Example: [GFGTABS] Python import re s = "Hello, we
    3 min read
  • Python - re.compile()
    In Python, re.compile() from the re module creates a regex object from a regular expression pattern. This object lets you perform operations like search(), match(), and findall(). In this article, we will understand about re.compile() method. [GFGTABS] Python import re # Compile the pattern pattern
    2 min read
  • re.subn() in Python
    re.subn() method in Python is used to search for a pattern in a string and replace it with a new substring. It not only performs the replacement but also tells us how many times the replacement was made. We can use this method when we need to replace patterns or regular expressions in text and get a
    3 min read
  • Python Data Types
    Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
    10 min read
  • re.MatchObject.span() Method in Python - regex
    re.MatchObject.span() method returns a tuple containing starting and ending index of the matched string. If group did not contribute to the match it returns(-1,-1). Syntax: re.MatchObject.span() Parameters: group (optional) By default this is 0. Return: A tuple containing starting and ending index o
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences