Find every phone number in a long text, check that an email address looks valid, pull dates and error levels out of a log file with a million lines. With find() and split(), each task takes a page of fragile code. A regular expression (regex) describes the shape of the text you are looking for in one short pattern, and the re module does the searching. Regular expressions are used everywhere: in text editors, databases, command-line tools and form validation on websites.
First steps: search, findall and raw strings
A pattern is a string written in a small special language. re.search(pattern, text) finds the first match anywhere in the text and returns a Match object (or None); .group() gives the matched text. re.findall returns a list of all matches. \d means “a digit” and + means “one or more”:
import re
text = 'Order 17 shipped on 2026-09-15, order 245 on 2026-09-20.'
print(re.search(r'\d+', text))
print(re.search(r'\d+', text).group())
print(re.findall(r'\d+', text))
print(re.findall(r'\d{4}-\d{2}-\d{2}', text))
print(re.search(r'cancelled', text))Expected output
<re.Match object; span=(6, 8), match='17'> 17 ['17', '2026', '09', '15', '245', '2026', '09', '20'] ['2026-09-15', '2026-09-20'] None
The pattern language
| Symbol | Meaning | Example |
|---|---|---|
. | any character except a newline | a.c → abc, a-c |
\d, \w, \s | digit, word character (letter, digit, _), whitespace | \d\d → 42 |
\D, \W, \S | the opposite: not a digit, not a word character, not whitespace | \S+ → 2026-09-15 |
[abc], [a-z], [^0-9] | one character from a set / range, a character not in the set | [aeiou] → e |
*, +, ? | 0 or more, 1 or more, 0 or 1 | colou?r → color, colour |
{n}, {n,m} | exactly n times, from n to m repetitions | \d{4} → 2026 |
^, $, \b | start and end of the string, word boundary | ^\d+$ |
a|b, (...) | alternative, group | (cat|dog)s |
\., \(, \+ | a special character taken literally: dot, bracket, plus | \+994 |
import re
print(re.findall(r'\w+', 'Salam, dünya! Привет, мир!'))
print(re.findall(r'\b\w{4}\b', 'Baku is a large city by the sea'))
print(re.findall(r'colou?r', 'color or colour?'))
print(re.findall(r'[A-Z]\w*', 'Aysel and Murad met Leyla'))Expected output
['Salam', 'dünya', 'Привет', 'мир'] ['Baku', 'city'] ['color', 'colour'] ['Aysel', 'Murad', 'Leyla']
In Python 3, \w understands Unicode: it matches letters of any alphabet, so Azerbaijani, Turkish and Cyrillic words are found as well. \b matches the invisible boundary between a word character and a non-word character, which is why \b\w{4}\b finds whole four-letter words and not pieces of longer ones.
Groups and named groups
Parentheses create a group — the part of the match you actually need. m.group(n) returns group n (group 0 is the whole match), and m.groups() returns them all as a tuple. Numbered groups quickly become confusing, so give them names with (?P<name>...) and read them as m['name'], or all at once with m.groupdict(). re.match only tries at the beginning of the string, which suits a single log line:
import re
line = '2026-09-15 14:02:11 ERROR [payment] card declined'
m = re.match(r'(\S+) (\S+) (\w+) \[(\w+)\] (.*)', line)
print(m.group(3), '|', m.group(5))
print(m.groups())
named = r'(?P<date>\S+) (?P<time>\S+) (?P<level>\w+) \[(?P<module>\w+)\] (?P<msg>.*)'
m = re.match(named, line)
print(m['level'], m['module'])
print(m.groupdict())Expected output
ERROR | card declined
('2026-09-15', '14:02:11', 'ERROR', 'payment', 'card declined')
ERROR payment
{'date': '2026-09-15', 'time': '14:02:11', 'level': 'ERROR', 'module': 'payment', 'msg': 'card declined'}If the pattern has groups, findall returns the groups instead of the whole match — as tuples when there are several. When you also need positions or named groups, loop with **finditer**, which yields Match objects lazily:
import re
phones = 'Aysel: +994 50 123 45 67, Murad: +994 55 765 43 21'
print(re.findall(r'\+994 (\d{2}) (\d{3} \d{2} \d{2})', phones))
for m in re.finditer(r'(?P<name>\w+): (?P<phone>\+994[\d ]+\d)', phones):
print(m['name'], '->', m['phone'], m.span())Expected output
[('50', '123 45 67'), ('55', '765 43 21')]
Aysel -> +994 50 123 45 67 (0, 24)
Murad -> +994 55 765 43 21 (26, 50)Greedy and lazy quantifiers
Quantifiers are greedy by default: .+ grabs as much text as it can and gives characters back only when the rest of the pattern would otherwise fail. Adding ? makes a quantifier lazy (.+?, *?, ??): it takes as little as possible. \1 is a backreference — it must match the same text as group 1, so the closing tag has to match the opening one:
import re
html = '<b>bold</b> and <i>italic</i>'
print(re.findall(r'<.+>', html))
print(re.findall(r'<.+?>', html))
print(re.findall(r'<(\w+)>(.*?)</\1>', html))Expected output
['<b>bold</b> and <i>italic</i>']
['<b>', '</b>', '<i>', '</i>']
[('b', 'bold'), ('i', 'italic')]Replace, split, flags and compile
re.sub(pattern, replacement, text) replaces every match. In the replacement string, \1, \2… insert groups; instead of a string you can also pass a function that receives the Match object and returns the new text. re.split splits by a pattern, for example by any mix of commas, semicolons and spaces:
import re
text = 'Call 050-123-45-67 or 055-765-43-21 today'
print(re.sub(r'\d{3}-\d{3}-\d{2}-\d{2}', '[hidden]', text))
print(re.sub(r'(\d{3})-(\d{3})-(\d{2})-(\d{2})', r'(\1) \2 \3 \4', text))
print(re.sub(r'\d+', lambda m: str(int(m.group()) * 2), 'a1 b22 c333'))
print(re.split(r'[,;\s]+', 'tea, coffee; juice water'))Expected output
Call [hidden] or [hidden] today Call (050) 123 45 67 or (055) 765 43 21 today a2 b44 c666 ['tea', 'coffee', 'juice', 'water']
Flags change how a pattern works: re.IGNORECASE (re.I) ignores letter case, re.MULTILINE (re.M) makes ^ and $ match at the start and end of every line, re.DOTALL (re.S) lets . match a newline too, and re.VERBOSE (re.X) allows spaces and # comments inside the pattern. A pattern used many times can be compiled once with re.compile:
import re
EMAIL = re.compile(r'''
[\w.+-]+ # user name
@
[\w-]+(\.[\w-]+)+ # domain with at least one dot
''', re.VERBOSE)
for address in ['leyla@mail.az', 'elvin.m@edu.gov.az', 'wrong@site', 'no at.com']:
print(f'{address:20} {bool(EMAIL.fullmatch(address))}')
log = 'ERROR one\ninfo two\nError three'
print(re.findall(r'^error.*$', log, re.IGNORECASE | re.MULTILINE))Expected output
leyla@mail.az True elvin.m@edu.gov.az True wrong@site False no at.com False ['ERROR one', 'Error three']
re.match— tries only at the beginning of the string.re.search— finds the first match anywhere.re.fullmatch— the whole string must match.re.findall/re.finditer— all matches (a list / an iterator of Match objects).re.sub,re.split— replace and split.
Example: checking a mobile number format
Problem
In a sign-up form, the phone must be written exactly as +994 50 123 45 67: the country code, a two-digit operator code and 3 + 2 + 2 digits, separated by single spaces. +994 501234567 and +994 50 123 45 67 ext must be rejected. Build the pattern.
Show solutionHide solution
Solution
+ is a special character, so we write \+994.The operator code is
\d{2}, the other parts are \d{3}, \d{2}, \d{2}, with exactly one space between them.Pattern:
r'\+994 \d{2} \d{3} \d{2} \d{2}'.re.fullmatch requires the whole string to match, so the trailing ext makes the number invalid.Exercise
Print the list of all dates in the format DD.MM.YYYY found in the text, then print the text again with every date rewritten as YYYY-MM-DD.
import re
text = 'Exams: 15.06.2026 (math), 18.06.2026 (physics), 22.06.2026 (history).'
# 1) print the list of all dates DD.MM.YYYY
# 2) print the text with every date rewritten as YYYY-MM-DDExpected output
['15.06.2026', '18.06.2026', '22.06.2026'] Exams: 2026-06-15 (math), 2026-06-18 (physics), 2026-06-22 (history).
Exercise
Build and print a dictionary {key: value} from the lines key = value (there may or may not be spaces around the equals sign). Comment lines starting with # must be skipped.
import re
config = 'host = localhost\nport=8080\n# comment\ndebug = true'
# build a dict {key: value} from the 'key = value' lines and print itExpected output
{'host': 'localhost', 'port': '8080', 'debug': 'true'}Remember
- Write patterns as raw strings
r'...';searchfinds the first match,findallfinds them all,fullmatchchecks the whole string. - Patterns are built from
\d \w \s, classes[...], quantifiers* + ? {n,m}and anchors^ $ \b. - Groups
(...)and named groups(?P<name>...)extract parts:m.group(1),m['name'],m.groupdict(). - Quantifiers are greedy; add
?for lazy matching (.+?). re.subaccepts\1backreferences and functions; the flagsre.I,re.M,re.Sandre.Xchange matching.