Python Regex Tutorial with Examples (re Module Cheat Sheet & Use Cases)

Python Regex Tutorial with Examples (re Module Cheat Sheet & Use Cases)

Python regular expressions (regex) provide a powerful way to search, match, extract, and manipulate text using patterns. Whether you're validating input, parsing logs, or cleaning data, regex helps automate complex string operations efficiently. In this guide, you’ll learn the most commonly used regex patterns in Python with practical examples.


Python Regex Syntax - Quick Cheat Sheet

PatternMeaningExampleMatches
.Any character except newlinea.cabc, a1c
^Start of string^HelloHello world
$End of stringend$the end
\dDigit (0–9)\d+123
\DNon-digit\D+abc
\wWord character (a-z, A-Z, 0-9, _)\w+test_1
\WNon-word character\W+@#!
\sWhitespace\s+space, tab
\SNon-whitespace\S+text123
[]Character set[aeiou]a, e
[^]Negated set[^0-9]a, b
``OR operator`cat
()Grouping(ab)+ab, abab
*0 or morea*"", a, aaa
+1 or morea+a, aaa
?0 or 1colou?rcolor, colour
{n}Exactly n timesa{3}aaa
{m,n}Between m and na{2,4}aa, aaa
{m,}At least m timesa{2,}aa, aaa
\bWord boundary\bcat\bcat (not scatter)
\BNon-word boundary\Bcatscatter
\AStart of string (strict)\AHiHi there
\ZEnd of string (strict)end\Zthe end

Common Python re Functions

FunctionDescriptionExample
re.search()Find first match anywherere.search(r'\d+', s)
re.match()Match only at beginningre.match(r'\d+', s)
re.findall()Find all matchesre.findall(r'\d+', s)
re.sub()Replace matchesre.sub(r'\s+', ' ', s)
re.split()Split string by patternre.split(r'\s+', s)
re.compile()Compile regex for reusere.compile(r'\d+')

Common Real-World Patterns

Use CasePatternExample
Email validation[\w\.-]+@[\w\.-]+'test@mail.com'
Phone number\d{3}-\d{3}-\d{4}123-456-7890
Extract numbers\d+45, 100
Remove extra spaces\s+"a b" → "a b"
Extract words\w+hello, world

Basic Python Regex Examples (Beginner Friendly)

Match a word or pattern in a string

In Python regex, matching a word or pattern in a string is one of the most common tasks. The re.search() function scans the entire string and returns the first match, making it useful for pattern detection, validation, and text analysis.

python
import re

text = "Learning python regex is fun"
pattern = r'python'

match = re.search(pattern, text)
if match:
    print("Match found:", match.group())

Output:

text
Match found: python

Find all matches using re.findall

To extract multiple values from a string, Python provides the re.findall() function. It returns all matching patterns, making it ideal for data extraction, parsing logs, and retrieving structured values.

python
import re

text = "Order IDs: 123, 456, 789"
pattern = r'\d+'

result = re.findall(pattern, text)
print(result)

Output:

text
['123', '456', '789']

Search vs match in Python regex

Understanding the difference between re.search() and re.match() is essential when working with Python regex. While re.search() scans the entire string, re.match() only checks from the beginning, which is useful for input validation and strict pattern matching.

  • re.search() → scans entire string
  • re.match() → checks only beginning
python
import re

text = "python is powerful"

print(re.search(r'python', text))  # Match
print(re.match(r'python', text))   # Match

text2 = "learn python"

print(re.search(r'python', text2))  # Match
print(re.match(r'python', text2))   # No match

Python Regex Character Classes and Patterns

Digits, words and whitespace (\d, \w, \s)

Character classes like \d, \w, and \s are shortcuts used in Python regex to match digits, words, and whitespace.

python
import re

text = "User123 logged in"

print(re.findall(r'\d+', text))  # Digits
print(re.findall(r'\w+', text))  # Words
print(re.findall(r'\s', text))   # Spaces

Custom character sets using []

Square brackets [] allow you to define a custom set of characters to match. This is useful when you want to match specific patterns or multiple possible characters at a certain position.

python
import re

text = "cat bat mat rat"

# Match words starting with c or b
print(re.findall(r'[cb]at', text))

Output:

text
['cat', 'bat']

Anchors (^ and $) explained with examples

Anchors such as ^ (start) and $ (end) are used to match patterns at specific positions in a string. They are commonly used in validation scenarios like checking full strings, file formats, or command outputs.

python
import re

text = "hello world"

# Starts with hello
print(re.search(r'^hello', text))

# Ends with world
print(re.search(r'world$', text))

Python Regex Groups and Capturing Data

Using parentheses () for grouping

Grouping using parentheses () allows you to treat multiple parts of a regex as a single unit and capture matched values.

python
import re

text = "abc123xyz"
pattern = r'(abc)(123)'

match = re.search(pattern, text)
print(match.groups())

Output:

text
('abc', '123')

Extract values using groups

Regex groups can be used to extract structured data such as dates, IDs, or formatted strings.

python
import re

text = "Date: 2026-03-17"
pattern = r'(\d{4})-(\d{2})-(\d{2})'

match = re.search(pattern, text)
year, month, day = match.groups()

print(year, month, day)

Named groups in Python regex

Named groups improve readability by assigning meaningful names to captured values.

python
import re

text = "Price: 499 INR"
pattern = r'(?P<amount>\d+)'

match = re.search(pattern, text)
print(match.group('amount'))

Python Regex Practical Use Cases (Real Scenarios)

Extract numbers from log files

Extracting numbers from logs is a common real-world use case of Python regex. This technique is widely used in log analysis, monitoring systems, debugging, and parsing application output where numerical values like error codes, timestamps, or IDs need to be captured.

python
import re

log = "Error code 500 at 10:45"
numbers = re.findall(r'\d+', log)

print(numbers)

Output:

text
['500', '10', '45']

Validate email format using regex

Validating email addresses using Python regex is a common requirement in form validation, user input processing, and web applications. Regex helps ensure that the email follows a standard format before storing or processing it.

python
import re

email = "user@example.com"
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'

if re.match(pattern, email):
    print("Valid email")

Validate phone numbers using regex

Phone number validation is another practical use case of regex, especially in data validation, APIs, and registration forms. Regex ensures the number follows a specific format such as XXX-XXX-XXXX.

python
import re

phone = "987-654-3210"
pattern = r'^\d{3}-\d{3}-\d{4}$'

if re.match(pattern, phone):
    print("Valid phone number")

Parse configuration or command output

Regex is very useful for extracting structured data from configuration files or command outputs. This is commonly used in DevOps, automation scripts, system administration, and parsing CLI outputs.

python
import re

config = "IP=192.168.1.10"
pattern = r'(\d+\.\d+\.\d+\.\d+)'

ip = re.search(pattern, config).group()
print(ip)

Output:

text
192.168.1.10

Python Regex Replace and Split Examples

Replace text using re.sub

Use re.sub() to replace matching patterns in a string.

python
import re

text = "This    is   a    test"
result = re.sub(r'\s+', ' ', text)

print(result)

Output:

text
This is a test

Split strings using regex patterns

Use re.split() to split a string using a pattern instead of a fixed delimiter.

python
import re

text = "apple,orange;banana grape"
result = re.split(r'[,\s;]+', text)

print(result)

Output:

text
['apple', 'orange', 'banana', 'grape']

Clean unwanted characters from text

Remove special characters and keep only alphanumeric values.

python
import re

text = "Hello@# World!! 123"
cleaned = re.sub(r'[^\w\s]', '', text)

print(cleaned)

Output:

text
Hello World 123

Python Regex Performance Tips

When to use re.compile

Use re.compile() when the same pattern is reused multiple times.

python
import re

pattern = re.compile(r'\d+')

text = "Order 123, Ref 456"
print(pattern.findall(text))

Avoiding common regex mistakes

  • Avoid overly complex patterns when simple string methods work
  • Always test regex with sample input
  • Use raw strings (r'') to prevent escape issues
python
pattern = r'\d+'  # Correct
pattern = '\\d+'  # Also works but less readable

Greedy vs non-greedy matching

  • Greedy (.*) → matches as much as possible
  • Non-greedy (.*?) → matches as little as possible
python
import re

text = "<tag>content</tag>"

# Greedy
print(re.findall(r'<.*>', text))

# Non-greedy
print(re.findall(r'<.*?>', text))

Output:

text
['<tag>content</tag>']
['<tag>', '</tag>']

Common Python Regex Mistakes (Important for Beginners)

Difference between re.match and re.search

python
import re

text = "learn python"

print(re.match(r'python', text))   # None
print(re.search(r'python', text))  # Match
  • re.match() → checks only beginning
  • re.search() → scans entire string

Escaping special characters properly

Special characters must be escaped using \.

python
import re

text = "price is $100"
pattern = r'\$100'

print(re.search(pattern, text))

Case sensitivity issues

Regex is case-sensitive by default. Use re.IGNORECASE when needed.

python
import re

text = "Python"

print(re.search(r'python', text))                    # No match
print(re.search(r'python', text, re.IGNORECASE))     # Match

Frequently Asked Questions

1. What is Python regex used for?

Python regex is used to search, match, extract, and manipulate text based on patterns. It is commonly used for validation, parsing logs, and data cleaning.

2. What is the difference between re.match and re.search?

re.match checks for a match only at the beginning of the string, while re.search scans the entire string for the first occurrence of the pattern.

3. What is re.findall in Python?

re.findall returns a list of all matching patterns found in a string, making it useful for extracting multiple values such as numbers or words.

4. When should I use re.compile in Python?

re.compile should be used when the same regex pattern is reused multiple times, as it improves performance by compiling the pattern once.

5. How to make regex case insensitive in Python?

You can use the re.IGNORECASE flag in functions like re.search or re.match to perform case-insensitive matching.

Summary

Python regex is a powerful tool for text processing, allowing you to search, extract, replace, and validate data efficiently. By understanding core patterns, functions like re.search, re.findall, and re.sub, and avoiding common mistakes, you can handle most real-world string manipulation tasks with ease.


Official Documentation

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.