How to Use Regular Expressions (Regex) in Python

Master Python regular expressions using the built-in re module. Learn string matching, pattern searching, finding all occurrences, and text replacement.

Try Code in Editor

Explanation

Regular expressions, commonly known as regex, are powerful templates used to match, search, and manipulate text strings based on specific patterns. Regex is widely used for email verification, phone number validation, web scraping, log parsing, and search-and-replace text tasks. Python provides full regex capabilities via the built-in `re` module.

The `re` module offers several primary functions depending on your goal. The `re.search(pattern, string)` function scans a string and returns a match object for the first occurrence of the pattern. The `re.match(pattern, string)` function is similar but only searches from the very beginning of the string. If you want to retrieve all instances of a pattern in a text block, `re.findall(pattern, string)` returns them as a list of strings.

Regex patterns utilize special metacharacters like `\d` for digits, `\w` for alphanumeric characters, `+` for one or more occurrences, and `*` for zero or more. When writing regex in Python, it is standard practice to use raw string literals (prefixed with `r`, such as `r"\d+"`) to prevent Python from treating backslashes as escape sequences.

Step-by-Step Implementation

  1. 1

    Import the built-in re module.

  2. 2

    Define regex search patterns using raw strings, like r"\d+" to avoid escape sequence issues.

  3. 3

    Use re.search() to find the first occurrence or re.findall() to collect all matching substrings.

  4. 4

    Use re.sub(pattern, replacement, string) to perform pattern-based replacements inside text.

Code Example

This script demonstrates validating format structures, extracting numeric tokens, and replacing matched patterns using the re module.

regex_demo.py
Try in Editor
import re

# 1. Searching for a pattern in text
log_message = "ERROR: Connection timeout at 04:30:15 on port 8080"
pattern_port = r"port \d+"

match = re.search(pattern_port, log_message)
if match:
    print("Found port pattern:", match.group())

# 2. Extracting all email addresses from a block of text
text = "Contact sales@pyrun.xyz or support@example.com for help."
pattern_email = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"

emails = re.findall(pattern_email, text)
print("Extracted emails:", emails)

# 3. Replacing patterns using re.sub
date_string = "2026/05/25"
# Replace slashes with dashes
clean_date = re.sub(r"/", "-", date_string)
print("Replaced Date:", clean_date)
Terminal Output
Found port pattern: port 8080
Extracted emails: ['sales@pyrun.xyz', 'support@example.com']
Replaced Date: 2026-05-25

Frequently Asked Questions

What is the benefit of using raw strings (r"...") for regex patterns?

Raw strings treat backslashes as literal characters instead of escape characters. For instance, r"\n" represents a backslash followed by an "n", whereas "\n" represents a newline character.

How do I check if a regex match was successful?

re.search() and re.match() return a match object if successful, or None if no match is found. You should check the result using a conditional statement (e.g., if match:).

Related How-To Guides

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.