Python RegEx

Regular expressions let you search, match, and transform text using compact pattern rules, and Python exposes them through the built-in re module.

What a regular expression is

A regular expression (often shortened to regex) is a small language for describing patterns in text. Instead of asking whether a string equals an exact value, you describe its shape, for example three digits followed by a dash and four more digits, and let the engine find every part of the text that fits. Python ships regular expression support in the standard library, so you only need to import the re module before you can start matching.

Importing re and running a first search

import re

text = "The event starts at 10:30 today"
match = re.search(r"\d\d:\d\d", text)

if match:
    print("Found a time:", match.group())
else:
    print("No time found")
Note: Write your patterns as raw strings by putting an r before the opening quote, as in r"\d\d". Without the r, Python tries to interpret backslash sequences itself before the regex engine ever sees them.

The core functions in re

Most day to day work uses a handful of functions from the module. Each one takes a pattern and the text to work on, and they differ mainly in what they return and how much of the string they consider.

FunctionWhat it does
re.search()Scans the whole string and returns a match object for the first place the pattern fits, or None.
re.match()Checks only whether the pattern fits at the very start of the string.
re.findall()Returns a list of every non-overlapping piece of text that matches.
re.finditer()Returns an iterator of match objects, useful when you also want positions.
re.sub()Replaces every match with a replacement string and returns the new text.
re.split()Splits the string wherever the pattern matches.

Common metacharacters and quantifiers

Patterns are built from ordinary characters, which match themselves, and metacharacters, which carry special meaning. Combining these building blocks with quantifiers lets you describe how many times something may repeat.

  • \d matches any digit, \w matches a letter, digit, or underscore, and \s matches whitespace.
  • The dot . matches any single character except a newline by default.
  • Square brackets such as [a-z] define a set of allowed characters.
  • The quantifiers * (zero or more), + (one or more), and ? (zero or one) control repetition.
  • Curly braces set an exact count, so \d{4} means exactly four digits.
  • The anchors ^ and $ tie a pattern to the start or end of the string.

Extracting all matches and using groups

import re

log = "orders: A123, B456, C789"

# Find every code: one letter followed by three digits
codes = re.findall(r"[A-Z]\d{3}", log)
print(codes)  # ['A123', 'B456', 'C789']

# Parentheses create groups you can pull out separately
email = "contact jane.doe@example.com please"
m = re.search(r"(\w+\.\w+)@(\w+\.\w+)", email)
if m:
    print("user:", m.group(1))
    print("domain:", m.group(2))

Replacing text with re.sub()

Substitution is one of the most practical uses of regular expressions. You give re.sub() a pattern, a replacement, and the text, and it returns a fresh string with every match swapped out. This is handy for redacting sensitive data or normalising messy input.

Masking phone numbers

import re

message = "Call 555-123-4567 or 555-987-6543"
hidden = re.sub(r"\d{3}-\d{3}-\d{4}", "[redacted]", message)
print(hidden)  # Call [redacted] or [redacted]
Note: When a pattern uses metacharacters to mean their literal selves, escape them with a backslash. To match a real dot, write \. rather than ., since a bare dot matches almost any character.

Exercise: Python RegEx

Which function scans through a string and returns a Match object for the first location the pattern matches, anywhere in the string?