Regular Expressions (the re module)

Published by Brayan K

A regular expression is a tiny pattern language for describing text — "any digit", "an email", "a word at the end of a line". Python's re module turns those patterns into powerful search-and-replace tools.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A pattern describes text

A regular expression, or regex, is a piece of text that describes a pattern. Instead of asking whether a string contains the exact word cat, you can ask whether it contains three digits, a dash, three digits, a dash and four digits. Python's patterns live in the re module, which comes with Python and is loaded with import re, as Operators and Expressions loaded math:

import re

text = "Call me on 555-123-4567 tomorrow."
m = re.search(r"\d{3}-\d{3}-\d{4}", text)
print(m.group())

# ✅ Expected output:
# 555-123-4567

\d is a digit, {3} repeats it three times, and each dash is just a dash. re.search() returns a match object, and .group() gives the text it matched. These are the building blocks:

\da Unicode decimal digit, not only 0 to 9 (see the FAQ)
\wa word character: a letter, a digit or an underscore
\swhitespace: a space, a tab or a line break
\D \W \Sthe opposite of each: anything but a digit, a word character or whitespace
.any character except a line break
[abc] [a-z]one of the characters listed, or one in the range
[^0-9]one character that isn't listed (^ inside the brackets means not)
* + ?the thing before it zero or more times, one or more times, or zero or one time (optional, as in colou?r)
{3} {2,4}exactly three times, or two to four times
^ $the start and the end of the text
\bthe edge of a word

Write every pattern as a raw string

Inside ordinary quotes, a backslash starts an escape sequence: "\n" is a line break, "\t" a tab and "\b" a backspace character. Patterns are full of backslashes, so a pattern in ordinary quotes can turn into something else before re ever sees it. An r before the quote makes a raw string, where a backslash is just a backslash:

import re

text = "cat category bobcat"
print(re.findall("\bcat\b", text))
print(re.findall(r"\bcat\b", text))
print(len("\b"), len(r"\b"))

# ✅ Expected output:
# []
# ['cat']
# 1 2

The first pattern looked for backspace characters, found none, and said nothing. The last line shows why: "\b" is one character and r"\b" two. Some patterns, such as "\d+", survive without the r because \d isn't an escape sequence, but Python 3.12 and later print a SyntaxWarning about them. Write the r every time. Strings, Bytes & Unicode Encodings covers escape sequences in full.

match, search and fullmatch

re.search() looks anywhere in the text, re.match() only at the start, and re.fullmatch() needs the whole text to match. Each returns a match object, which prints with the span of positions it covered, or None:

import re

print(re.match(r"\d{4}", "Year 2024"))
print(re.search(r"\d{4}", "Year 2024"))
print(re.match(r"\d{4}", "20245"))
print(re.fullmatch(r"\d{4}", "20245"))
print(re.fullmatch(r"\d{4}", "2024"))

# ✅ Expected output:
# None
# <re.Match object; span=(5, 9), match='2024'>
# <re.Match object; span=(0, 4), match='2024'>
# None
# <re.Match object; span=(0, 4), match='2024'>

match() is happy with 20245 because it starts with four digits; only fullmatch() notices the fifth. That matters when you check what someone typed:

import re

for pin in ["2024", "20245", "12a4"]:
    print(pin, bool(re.match(r"\d{4}", pin)), bool(re.fullmatch(r"\d{4}", pin)))

# ✅ Expected output:
# 2024 True True
# 20245 True False
# 12a4 False False

A pattern between ^ and $ comes close to fullmatch(), but $ also matches just before a line break at the very end, so a PIN typed with a line break after it would pass. Use fullmatch() to check a whole value.

No match is None

When search(), match() or fullmatch() finds nothing, it returns None, and None has no .group():

import re

m = re.search(r"\d+", "no digits here")
print(m)
print(m.group())

# ✅ Expected output:
# None

# ✅ Expected error:
# AttributeError: 'NoneType' object has no attribute 'group'

Check the result before using it. None counts as false in an if, and a match object as true:

import re

for text in ["Order 4521", "no order yet"]:
    m = re.search(r"\d+", text)
    if m:
        print(text, "->", m.group())
    else:
        print(text, "-> no number")

# ✅ Expected output:
# Order 4521 -> 4521
# no order yet -> no number

None & NoneType covers None in general. findall() is different: it returns a list, which is empty when nothing matches.

Groups pull out the pieces

Round brackets make a group. After a match, .group(1) is the text the first group matched and .groups() gives them all. A named group, written (?P<name>...), can be read by its name:

import re

date = "Due 2024-01-15"
m = re.search(r"(\d{4})-(\d{2})-(\d{2})", date)
print(m.group(0), m.group(1), m.group(3))
print(m.groups())

m = re.search(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})", date)
print(m.group("month"))
print(m.groupdict())

line = "2024-01-15 ERROR   disk full"
m = re.match(r"(\d{4}-\d{2}-\d{2}) (\w+)\s+(.*)", line)
print(m.groups())

# ✅ Expected output:
# 2024-01-15 2024 15
# ('2024', '01', '15')
# 01
# {'year': '2024', 'month': '01', 'day': '15'}
# ('2024-01-15', 'ERROR', 'disk full')

group(0) is the whole match. In the last pattern, \s+ takes the run of spaces and (.*) the rest of the line. A pattern checks a date's shape, not whether the date exists: 2024-13-45 fits it just as well. Dates & Times (datetime) reads real dates.

Groups also change what findall() returns: the whole matches when the pattern has no group, what the group matched when it has one, and tuples when it has several:

import re

text = "Ada: 10-20, Ben: 30-40"
print(re.findall(r"\d+-\d+", text))
print(re.findall(r"(\d+)-\d+", text))
print(re.findall(r"(\d+)-(\d+)", text))

# ✅ Expected output:
# ['10-20', '30-40']
# ['10', '30']
# [('10', '20'), ('30', '40')]

So brackets you only meant for repeating a part can hide the answer. To repeat a part without capturing it, start the brackets with (?::

import re

log = "from 192.168.0.1 and 10.0.0.5"
print(re.findall(r"\d{1,3}(\.\d{1,3}){3}", log))
print(re.findall(r"\d{1,3}(?:\.\d{1,3}){3}", log))

# ✅ Expected output:
# ['.1', '.5']
# ['192.168.0.1', '10.0.0.5']

findall() gives text, not numbers

Every match is text, even when it's all digits, so arithmetic on the matches fails:

import re

nums = re.findall(r"\d+", "3 apples, 12 pears")
print(nums)
print(sum(nums))

# ✅ Expected output:
# ['3', '12']

# ✅ Expected error:
# TypeError: unsupported operand type(s) for +: 'int' and 'str'

Turn each match into a number first. A generator expression or a list comprehension, from Comprehensions, does it in one line:

import re

nums = re.findall(r"\d+", "3 apples, 12 pears")
print(sum(int(n) for n in nums))
print([int(n) for n in nums])

# ✅ Expected output:
# 15
# [3, 12]

Some characters mean something else

In a pattern, . matches any character and $ means the end, and + * ? ( ) [ ] { } ^ | and \ all have jobs too. To match one of them as itself, put a backslash before it, or let re.escape() add the backslashes:

import re

print(re.findall(r"3.14", "3.14 and 3x14"))
print(re.findall(r"3\.14", "3.14 and 3x14"))
print(re.split(".", "a.b.c"))
print(re.split(r"\.", "a.b.c"))
print(re.escape("3.14"))
print(re.findall("$5", "cost $5"), re.findall(r"\$5", "cost $5"))

# ✅ Expected output:
# ['3.14', '3x14']
# ['3.14']
# ['', '', '', '', '', '']
# ['a', 'b', 'c']
# 3\.14
# [] ['$5']

re.split(".", "a.b.c") cut at every character and left six empty strings. A pattern that can't be read at all, such as an opening bracket with no closing one, stops with an error: re.search("(", "a") gives "missing ), unterminated subpattern at position 0". The error is called re.error up to Python 3.12 and re.PatternError from 3.13, and Exception Handling shows how to catch it. A wildcard such as *.txt isn't a regex at all; Finding Files with glob & fnmatch covers those.

Greedy and lazy

*, + and the other repeats are greedy: they take as much text as they can while the pattern still matches. A ? after one makes it lazy, taking as little as possible. Or say exactly what may repeat:

import re

html = "<b>bold</b> and <i>italic</i>"
print(re.findall(r"<.*>", html))
print(re.findall(r"<.*?>", html))
print(re.findall(r"<[^>]*>", html))

# ✅ Expected output:
# ['<b>bold</b> and <i>italic</i>']
# ['<b>', '</b>', '<i>', '</i>']
# ['<b>', '</b>', '<i>', '</i>']

[^>]* means any characters except >, so it can't run past the end of a tag. Real web pages are better read with a parser, which Web Scraping with BeautifulSoup uses.

Replacing, splitting and ignoring case

re.sub() replaces every match and returns new text. In the replacement, \1, \2 and so on put back what each group matched, which reorders a date in one call:

import re

text = "Due 2024-01-15, paid 2024-02-03"
print(re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\3/\2/\1", text))
print(text)

# ✅ Expected output:
# Due 15/01/2024, paid 03/02/2024
# Due 2024-01-15, paid 2024-02-03

Here are two jobs String Methods handed over: removing characters other than word characters and whitespace, and splitting at more than one separator. The first pattern preserves underscores because \w includes them:

import re

print(re.sub(r"[^\w\s]", "", "Hi, there! Ready?"))
print(re.split(r"[,;]\s*", "tea, jam;bread; milk"))
print("tea, jam;bread".split(","))

# ✅ Expected output:
# Hi there Ready
# ['tea', 'jam', 'bread', 'milk']
# ['tea', ' jam;bread']

[^\w\s] is one character that is neither a word character nor whitespace. A pattern matches capitals exactly, unless you pass flags=re.IGNORECASE:

import re

print(re.findall(r"python", "Python, python, PYTHON", flags=re.IGNORECASE))
print(re.sub(r"a", "o", "Banana", flags=re.IGNORECASE))

# ✅ Expected output:
# ['Python', 'python', 'PYTHON']
# Bonono

Pass the flag by name, as here. Given without flags=, re.IGNORECASE lands in re.sub()'s count argument instead, and quietly limits how many matches are replaced; Python 3.13 also prints a DeprecationWarning about it.

A worked example: reading a log

Patterns get long. re.VERBOSE lets you spread one over several lines with a comment on each part, and re.compile() gives it a name you can reuse. (re also remembers recently used patterns by itself, so compiling is about clarity more than speed.) The last line of the log doesn't fit the pattern, so match() returns None and the loop skips it:

import re

LOG = """2024-01-15 09:12:03 ERROR db  connection refused
2024-01-15 09:12:44 WARN  api slow response 1420ms
2024-01-15 09:13:01 ERROR api timeout after 30s
2024-01-15 09:15:22 INFO  web deploy finished
-- end of log --"""

# re.VERBOSE lets you spread a pattern over lines and comment each piece.
# Whitespace inside the pattern is ignored, so you write \s+ where you mean a gap.
LINE = re.compile(r"""
    (?P<date>\d{4}-\d{2}-\d{2})     # 2024-01-15
    \s+
    (?P<time>\d{2}:\d{2}:\d{2})     # 09:12:03
    \s+
    (?P<level>[A-Z]+)               # ERROR / WARN / INFO
    \s+
    (?P<service>\w+)                # db / api / web
    \s+
    (?P<message>.+)                 # everything left on the line
""", re.VERBOSE)

errors = 0
for line in LOG.splitlines():
    m = LINE.match(line)          # match() anchors at the start of the line
    if m is None:                 # the "-- end of log --" line matches nothing
        continue
    row = m.groupdict()           # named groups come back as a plain dict
    if row["level"] == "ERROR":
        errors += 1
        print(f"{row['time']}  {row['service']:4} {row['message']}")

print("Errors found:", errors)

# re.sub replaces every match; \d+ms means "one or more digits then 'ms'"
print(re.sub(r"\d+ms", "[REDACTED]", LOG.splitlines()[1]))

# ✅ Expected output:
# 09:12:03  db   connection refused
# 09:13:01  api  timeout after 30s
# Errors found: 2
# 2024-01-15 09:12:44 WARN  api slow response [REDACTED]

The triple quotes make one string that runs over several lines, and splitlines() cuts it back into lines. File Handling reads a real log from a file.

Practice: check a whole product code

Product codes are two capital letters, a dash and four digits. Fill the two blanks with the values in the hint lines, in order, so that only AB-1234 passes.

import re

codes = ["AB-1234", "ab-1234", "AB-12345", "AB-12"]
for code in codes:
    ok = re.___(r"[A-Z]{2}-\d{___}", code)
    print(code, bool(ok))

# 👉 replace ___ with fullmatch
# 👉 replace ___ with 4

# ✅ Expected output:
# AB-1234 True
# ab-1234 False
# AB-12345 False
# AB-12 False

On your own: tidy phone numbers

Phone numbers arrive as 555-123-4567, (555) 123 4567, 555.123.456 and 555 123 45678. For each one, remove everything that isn't a digit with one re.sub(), then print it in the 555-123-4567 style when exactly ten digits remain, or invalid when they don't. The first two should come out the same. Then compare with this solution:

import re

entries = ["555-123-4567", "(555) 123 4567", "555.123.456", "555 123 45678"]
for entry in entries:
    digits = re.sub(r"\D", "", entry)
    m = re.fullmatch(r"(\d{3})(\d{3})(\d{4})", digits)
    if m:
        print(entry, "->", "-".join(m.groups()))
    else:
        print(entry, "-> invalid")

# ✅ Expected output:
# 555-123-4567 -> 555-123-4567
# (555) 123 4567 -> 555-123-4567
# 555.123.456 -> invalid
# 555 123 45678 -> invalid

\D is anything that isn't a digit. fullmatch() with three groups both checks that there are exactly ten digits and splits them, and join() puts the dashes back.

Practice quiz

What does re.findall("\bcat\b", "the cat sat") give, with no r before the pattern?

  • ['cat']
  • []
  • ['the cat sat']
  • An error

Answer: []. Without the r, \b is a backspace character, so the pattern looks for backspaces and finds none.

What do bool(re.match(r"\d{4}", "20245")) and bool(re.fullmatch(r"\d{4}", "20245")) give?

  • True False
  • True True
  • False False
  • False True

Answer: True False. match() only needs four digits at the start; fullmatch() needs the whole text to be four digits.

What does re.findall(r"(\d+)-(\d+)", "1-2 and 30-40") give?

  • ['1-2', '30-40']
  • ['1', '2', '30', '40']
  • [('1', '2'), ('30', '40')]
  • [('1-2',), ('30-40',)]

Answer: [('1', '2'), ('30', '40')]. With two groups, findall gives one tuple per match, holding what each group matched.

What does re.split(r"[,;]\s*", "a, b;c") give?

  • ['a, b;c']
  • ['a', ' b', 'c']
  • ['a', 'b;c']
  • ['a', 'b', 'c']

Answer: ['a', 'b', 'c']. It splits at a comma or a semicolon and takes the spaces after it too.

What does re.findall(r"3.14", "3.14 3x14") give?

  • ['3.14', '3x14']
  • ['3.14']
  • []
  • ['3x14']

Answer: ['3.14', '3x14']. An unescaped dot matches any character, including x. Write 3\.14 for a real dot.

What does re.search(r"\d+", "none").group() do?

  • It prints None
  • It gives an empty string
  • It stops with AttributeError: 'NoneType' object has no attribute 'group'
  • It gives 0

Answer: It stops with AttributeError: 'NoneType' object has no attribute 'group'. search() returns None when nothing matches, and None has no .group().

What does re.findall(r"<.*>", "<b>x</b>") give?

  • ['<b>', '</b>']
  • ['<b>x</b>']
  • ['x']
  • ['<b>', 'x', '</b>']

Answer: ['<b>x</b>']. .* is greedy: it runs from the first < to the last >. Use .*? or [^>]* for each tag.

What does sum(re.findall(r"\d+", "3 and 4")) do?

  • It gives 7
  • It gives 34
  • It gives ['3', '4']
  • It stops with TypeError: unsupported operand type(s) for +: 'int' and 'str'

Answer: It stops with TypeError: unsupported operand type(s) for +: 'int' and 'str'. findall gives text. Turn each match into a number first: sum(int(n) for n in ...).

Continue this course

Frequently asked questions

Why does \d match ٣?

In Python 3, \d matches any decimal digit Unicode knows, such as the Arabic-Indic ٣ or a full-width 5, not only 0 to 9, and \w matches letters such as é. Pass flags=re.ASCII, or write [0-9], when only the ASCII digits will do.

Is match/case related to re.match()?

No. match/case is a statement that compares a value against shapes such as a list of two items or a dictionary with a given key; re.match() matches a text pattern. Structural Pattern Matching (match/case) covers the statement.

Should I catch re.error or re.PatternError?

Either works on recent Pythons. Python 3.13 renamed the error for a pattern that can't be read to re.PatternError and kept re.error as another name for it, so except re.error: works on every version.

Related lessons