IntermediateLesson 40 of 4714 min read12 runnable examples
A regular expression (regex) is a small pattern language for finding and extracting text: every email address in a page, every number in a log, every date in a report. Python’s re module runs these patterns for you.
In this lesson
search, match, fullmatch, findall and sub
Character classes and quantifiers
Capture groups and named groups
Flags and compiled patterns
Your first regex
Write patterns as raw strings (r"...") so backslashes reach the regex engine unchanged.
Python
importretext="Order 4521 shipped on 2026-11-02, order 4522 pending"match=re.search(r"\d+",text)# first run of digitsprint(match.group(),match.start(),match.end())print(re.findall(r"\d+",text))# every run of digits
Output
4521 6 10
['4521', '2026', '11', '02', '4522']
The main functions
Function
Returns
re.search(p, s)
First match anywhere, or None
re.match(p, s)
Match only at the start, or None
re.fullmatch(p, s)
Match only if the whole string fits
re.findall(p, s)
List of all matches (or of groups)
re.finditer(p, s)
Iterator of match objects
re.sub(p, repl, s)
String with matches replaced
re.split(p, s)
List split on the pattern
Python
importreprint(re.match(r"\d+","123abc"))print(re.match(r"\d+","abc123"))print(bool(re.fullmatch(r"\d{6}","411001")),bool(re.fullmatch(r"\d{6}","41100")))print(re.sub(r"\s+"," ","too many spaces"))print(re.split(r"[,;]\s*","a, b;c ,d"))
Output
<re.Match object; span=(0, 3), match='123'>
None
True False
too many spaces
['a', 'b', 'c ', 'd']
Tip
Always check for None before calling .group() on the result of search or match: if m := re.search(p, s): print(m.group()).
importreprint(re.sub(r"(\d{4})-(\d{2})-(\d{2})",r"\3/\2/\1","Due 2026-11-02 and 2026-12-25"))
Output
Due 02/11/2026 and 25/12/2026
Flags
Python
importretext="Python\npython\nPYTHON"print(re.findall(r"python",text,flags=re.IGNORECASE))print(re.findall(r"^p\w+",text,flags=re.I|re.M))# ^ at every line start
\d matches one digit at a time, \d+ matches each run of digits, and sub replaces each run with #.
Quick quiz
1. What is the difference between re.match and re.search?
Use search unless you specifically need the start.
2. What does \d{3} match?
{3} repeats the previous item exactly three times.
3. Why write patterns as raw strings like r"\d+"?
Without r, "\b" would be a backspace character instead of a word boundary.
4. What does re.findall return when the pattern has one group?
With groups, findall returns the captured parts.
Exercise: extract phone numbers
Find all 10-digit Indian mobile numbers (starting with 6, 7, 8 or 9) in the text, allowing an optional +91 and a space or hyphen after the first 5 digits.
Input
"Call 98765-43210 or +91 91234 56789; old: 12345 67890"
The pattern requires the first digit to be 6-9, so “12345 67890” is skipped. re.sub(r"\D", "", ...) removes everything that is not a digit, and [-10:] drops the 91 prefix.
Program Practice
20 Python RegEx Programs
Every program has a step-by-step explanation, colour-coded code and its real output.
re is the standard-library module for regular expressions. It provides search, match, findall, sub, split and compile.
How do I extract numbers from a string in Python?
re.findall(r"\d+", text) returns every run of digits as strings. Use r"-?\d+(?:\.\d+)?" to include negatives and decimals.
What does re.compile do?
It turns a pattern into a reusable object. It makes code clearer when the same pattern is used many times; Python also caches recently used patterns automatically.
How do I make a regex case-insensitive?
Pass flags=re.IGNORECASE (or re.I), or start the pattern with (?i).