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.
A first search
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.
import re
text = "Order 4521 shipped to Delhi"
found = re.search(r"\d+", text)
print(found)
print(found.group())
print(found.start(), found.end())<re.Match object; span=(6, 10), match='4521'>
4521
6 10The 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:
found = re.search(r"\d+", "no numbers here")
if found:
print("found", found.group())
else:
print("nothing found")nothing foundRaw 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.
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)))AB-1234 True
xy-99 False
QR-5678 True
AB1234 FalseHere ^ 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.
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")))True
False
True
FalseFinding 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.
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())['3', '12', '150']
165
3 at 5
12 at 13
150 at 30Remember 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.
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())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.
m = re.fullmatch(r"([\w.]+)@([\w.]+)", "asha@example.com")
print(m.group(1), "at", m.group(2))asha at example.comReplacing and splitting
re.sub(pattern, replacement, text) replaces every match. The replacement can refer back to groups with \1, \2 and so on.
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"))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.
html = "<b>sale</b> and <b>new</b>"
print(re.findall(r"<b>.*</b>", html))
print(re.findall(r"<b>.*?</b>", html))['<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.
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"))['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. Writer"\d". - Using the result without checking for
None.re.search(...).group()fails with anAttributeErrorwhen nothing matches. - Mixing up
matchandsearch.matchonly 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 withint()orfloat(). - Reaching for a regex when a string method is enough.
startswith,splitandinare 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
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))['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.