You are currently viewing Python Regex Cheat Sheet – Regular Expressions in Python

Python Regex Cheat Sheet – Regular Expressions in Python

Regular expressions, often referred to as regex or regexp, which is a powerful tool for pattern matching and text manipulation. In python, the ‘re’ module provides the support for using regex and allows developers to perform complex string operations with ease. Whether you are a beginner or a experienced developer, having a cheat sheet in python can be greatly helpful.

Basic Patterns

Literal CharactersDescription
abcMatches the exact string ‘abc’.

Example

import re

pattern = r'python'
text = 'Python is a powerful programming language.'

match = re.search(pattern, text, re.IGNORECASE)
print(match.group())  # Output: Python

Character Classes

PatternDescription
[aA]Matches either ‘a’ or ‘A’.
[0-9]Matches any digit.
[^a]Matches any character except ‘a’.

Example

import re

pattern = r'[aeiou]'
text = 'Regular expressions are powerful.'

matches = re.findall(pattern, text, re.IGNORECASE)
print(matches)  # Output: ['e', 'u', 'a', 'e', 'e', 'i', 'o', 'a', 'i', 'o', 'e']

Special Characters

PatternDescription
.Matches any character except a newline.
\dMatches any digit.
\wMatches any alphanumeric character.
\sMatches any whitespace character.
\bMatches a word boundary.

Example

import re

pattern = r'\d+'
text = 'The price of the product is $50.99.'

match = re.search(pattern, text)
print(match.group())  # Output: 50

Quantifiers

PatternDescription
*Matches 0 or more occurrences.
+Matches 1 or more occurrences.
?Matches 0 or 1 occurrence.
{n}Matches exactly n occurrences.
{n,}Matches n or more occurrences.
{n,m}Matches between n and m occurrences.

Example

import re

pattern = r'\b\w{3,5}\b'
text = 'Regex is a powerful tool for text processing.'

matches = re.findall(pattern, text)
print(matches)  # Output: ['Regex', 'tool', 'text']

Anchors

PatternDescription
^Matches the start of a string.
$Matches the end of a string.

Example

import re

pattern = r'^Python'
text = 'Python is a popular programming language.'

match = re.search(pattern, text)
print(match.group())  # Output: Python

Groups and Capturing

PatternDescription
(abc)Matches and captures ‘abc’.
(?:abc)Matches ‘abc’ but does not capture.

Example

import re

pattern = r'(\d{2})-(\d{2})-(\d{4})'
text = 'Date: 27-11-2023'

match = re.search(pattern, text)
print(match.group(1))  # Output: 27
print(match.group(2))  # Output: 11
print(match.group(3))  # Output: 2023

Character Escapes

PatternDescription
\Escapes a special character, e.g., \. matches a literal period.

Example

import re

pattern = r'The cost is \$\d+'
text = 'The cost is $50.'

match = re.search(pattern, text)
print(match.group())  # Output: The cost is $50

Lookahead and Lookbehind

PatternDescription
x(?=y)Matches ‘x’ only if followed by ‘y’.
x(?!y)Matches ‘x’ only if not followed by ‘y’.
(?<=y)xMatches ‘x’ only if preceded by ‘y’.
(?<!y)xMatches ‘x’ only if not preceded by ‘y’.

Example

import re

pattern = r'\b\w+(?=\sprogramming)'
text = 'Python is a powerful programming language.'

matches = re.findall(pattern, text)
print(matches)  # Output: ['powerful']

Flags

FlagDescription
re.IGNORECASE or re.ICase-insensitive matching.
re.MULTILINE or re.MAllows ‘^’ and ‘$’ to match the start/end of each line.
re.DOTALL or re.SAllows ‘.’ to match any character, including newline.

Example

import re

pattern = r'(?i)python'
text = 'Python is a versatile programming language.'

match = re.search(pattern, text)
print(match.group())  # Output: Python

Common Functions

FunctionDescription
re.search(pattern, string)Searches for the first occurrence of the pattern in the string.
re.match(pattern, string)Matches the pattern only at the beginning of the string.
re.findall(pattern, string)Returns a list of all occurrences of the pattern in the string.
re.finditer(pattern, string)Returns an iterator of match objects for all occurrences.
re.sub(pattern, replacement, string)Replaces occurrences of the pattern with the replacement in the string.

Example

import re

# Example 1: re.search
pattern = r'\d{3}-\d{2}-\d{4}'
text = 'SSN: 123-45-6789'
match = re.search(pattern, text)
print(match.group())  # Output: 123-45-6789

# Example 2: re.findall
pattern = r'\b\d+\b'
text = 'There are 42 apples and 17 oranges.'
matches = re.findall(pattern, text)
print(matches)  # Output: ['42', '17']

Leave a Reply