Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 65 of 70

Python regular expressions with the re module

Search, match, extract and replace text patterns in Python with the re module, using character classes, quantifiers, groups and raw strings.

15 minGoing furtherUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonWrite simple regular expressions and use re.search, findall, sub and split to find, extract and replace patterns in text.

After this lesson you will be able to find, pull out and replace pieces of text by describing their shape rather than their exact characters. For example, "a run of digits" or "a word followed by an at sign". Python does this with the re module in the standard library. Regular expressions are a small language of their own, so this lesson covers the pieces you will use most.

re.search(pattern, text) looks through the text for the first place where the pattern fits. It returns a match object if it finds one, and None if it does not.

python
import re

text = "Order 4521 shipped to Delhi"
found = re.search(r"\d+", text)

print(found)
print(found.group())
print(found.start(), found.end())
result
<re.Match object; span=(6, 10), match='4521'>
4521
6 10

The pattern \d+ means "one or more digits". group() gives the matched text, and start and end give its position.

Because search returns None when nothing matches, check before using the result:

python
found = re.search(r"\d+", "no numbers here")
if found:
    print("found", found.group())
else:
    print("nothing found")
result
nothing found

Raw strings

Patterns are full of backslashes, and Python also uses backslashes for escapes like \n. Put an r before the opening quote to make a raw string, where backslashes are kept as they are. Get into the habit of writing every pattern as r"...".

The building blocks

Some characters stand for themselves, and others are special:

Piece Meaning
\d and \D a digit, and anything that is not a digit
\w and \W a letter, digit or underscore, and the opposite
\s and \S whitespace, and the opposite
. any character except a newline
[abc] or [a-z] one character from the set or range
^ and $ the start and the end of the text
*, +, ? zero or more, one or more, zero or one
{3} or {2,4} exactly three, or between two and four

To match a special character literally, put a backslash before it, as in \. for a real full stop.

python
codes = ["AB-1234", "xy-99", "QR-5678", "AB1234"]
pattern = r"^[A-Z]{2}-\d{4}$"

for code in codes:
    print(code, bool(re.search(pattern, code)))
result
AB-1234 True
xy-99 False
QR-5678 True
AB1234 False

Here ^ and $ pin the pattern to the whole string, so it accepts only two capital letters, a hyphen and exactly four digits. The function re.fullmatch(pattern, text) does the same job without the anchors.

match, search and fullmatch

re.match only tries at the very start of the text, re.search tries everywhere, and re.fullmatch requires the whole text to fit.

python
print(bool(re.match(r"\d+", "42 items")))
print(bool(re.match(r"\d+", "items: 42")))
print(bool(re.search(r"\d+", "items: 42")))
print(bool(re.fullmatch(r"\d+", "42 items")))
result
True
False
True
False

Finding every match

re.findall returns a list of all non-overlapping matches. re.finditer returns match objects one by one, which is handy when you want the positions.

python
log = "sold 3 pens, 12 notebooks and 150 stickers"

print(re.findall(r"\d+", log))
numbers = [int(n) for n in re.findall(r"\d+", log)]
print(sum(numbers))

for m in re.finditer(r"\d+", log):
    print(m.group(), "at", m.start())
result
['3', '12', '150']
165
3 at 5
12 at 13
150 at 30

Remember that findall returns strings, so convert them yourself if you want to calculate with them.

Groups: extracting parts

Round brackets create groups so you can pull out parts of a match. Naming them with (?P<name>...) makes the code easier to read.

python
line = "2026-11-14 Asha borrowed Dune"
m = re.search(r"(\d{4})-(\d{2})-(\d{2})", line)

print(m.group(0))
print(m.groups())

m2 = re.search(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})", line)
print(m2.group("month"))
print(m2.groupdict())
result
2026-11-14
('2026', '11', '14')
11
{'year': '2026', 'month': '11', 'day': '14'}

A common use is checking and splitting an email-like string. This simple pattern is fine for demonstrations, but real email validation is much more complicated, so do not rely on it for that.

python
m = re.fullmatch(r"([\w.]+)@([\w.]+)", "asha@example.com")
print(m.group(1), "at", m.group(2))
result
asha at example.com

Replacing and splitting

re.sub(pattern, replacement, text) replaces every match. The replacement can refer back to groups with \1, \2 and so on.

python
print(re.sub(r"\d", "#", "Card 4521 9987"))
print(re.sub(r"(\w+)@(\w+)\.com", r"\2: \1", "asha@example.com"))
print(re.split(r"[,;]\s*", "pens, ink;paper,  glue"))
result
Card #### ####
example: asha
['pens', 'ink', 'paper', 'glue']

Greedy and lazy matching

Quantifiers are greedy: they take as much as they can. Adding ? after them makes them lazy, taking as little as possible.

python
html = "<b>sale</b> and <b>new</b>"
print(re.findall(r"<b>.*</b>", html))
print(re.findall(r"<b>.*?</b>", html))
result
['<b>sale</b> and <b>new</b>']
['<b>sale</b>', '<b>new</b>']

Flags and compiled patterns

Pass re.IGNORECASE to ignore case. If you use one pattern many times, re.compile builds it once.

python
print(re.findall(r"dune", "Dune, DUNE and dune", re.IGNORECASE))

phone = re.compile(r"\d{3}-\d{4}")
print(phone.findall("call 555-1234 or 555-9876"))
result
['Dune', 'DUNE', 'dune']
['555-1234', '555-9876']

Common mistakes

  • Forgetting the raw string. "\d" often works by luck, but it is fragile and newer Python versions warn about it. Write r"\d".
  • Using the result without checking for None. re.search(...).group() fails with an AttributeError when nothing matches.
  • Mixing up match and search. match only looks at the start, which surprises many beginners.
  • Forgetting that . is special. To match a real full stop, write \..
  • Expecting numbers from findall. It returns strings, so convert with int() or float().
  • Reaching for a regex when a string method is enough. startswith, split and in are simpler and clearer for plain checks.

Exercise

Given the text "Contact: sales@shop.com, help@shop.com, phone 555-0101", use re.findall to list all email-like words (letters, then @, then letters, a dot and letters), and use re.sub to replace every digit in the text with *.

Show one possible answer
python
import re

text = "Contact: sales@shop.com, help@shop.com, phone 555-0101"

print(re.findall(r"[a-z]+@[a-z]+\.[a-z]+", text))
print(re.sub(r"\d", "*", text))
result
['sales@shop.com', 'help@shop.com']
Contact: sales@shop.com, help@shop.com, phone ***-****

Next: dates and times is another topic where Python's standard library saves you a lot of work. A video version of this topic is in the video course at /learn/python/.

Key points

  • A regular expression is a pattern that describes text. Python's re module searches for it with search, match, fullmatch, findall, finditer, sub and split.
  • Write patterns as raw strings, r'\d+', so backslashes reach the regex engine untouched.
  • Common pieces: \d digit, \w word character, \s whitespace, . any character, [abc] a set, + one or more, * zero or more, ? optional, {n} exactly n.
  • Parentheses make groups to extract parts of a match, and (?P<name>...) names them.
  • search returns None when nothing matches, so check before using the result.

Check yourself

1. Why are regular expressions usually written as raw strings, such as r'\d+'?
2. What does re.search(r'\d+', 'Order 4521 shipped') return?
3. What is the difference between re.match and re.search?
4. What does re.findall(r'\d+', 'a1b22c333') return?