Regular expressions with re
Find, extract, validate and replace text patterns with Python's re module, using groups, flags and practical patterns.
In this lesson you will
- Write patterns with character classes and quantifiers
- Use search, findall, sub and split
- Capture parts of a match with groups
- Validate whole strings with fullmatch
About 24 minutes
Find anything in text
You have ten thousand lines of customer data and need every email address. Or every phone number, every date, every hashtag. Writing loops with split and find would be painful. A regular expression (regex) is a tiny pattern language that describes the shape of text, and Python’s re module finds it in an instant. Regex looks cryptic at first, but it has only a handful of building blocks. Learn them one at a time and it becomes a superpower.
Searching for a pattern
import re
text = "Order 4821 shipped on 2026-03-15 to Lahore. Order 4822 is pending."
print(re.search(r"\d+", text)) # first match, as a match object
print(re.search(r"\d+", text).group()) # the matched text
print(re.findall(r"\d+", text)) # every match, as a list
print(re.findall(r"Order \d+", text))
print(bool(re.search(r"pending", text)))
print(re.search(r"refund", text)) # no match gives None
<re.Match object; span=(6, 10), match='4821'>
4821
['4821', '2026', '03', '15', '4822']
['Order 4821', 'Order 4822']
True
None
Always write patterns as raw strings: r"...". In a raw string a backslash stays a backslash, which is exactly what regex needs. re.search finds the first match anywhere, re.findall returns all of them, and no match gives None.
The building blocks
| Pattern | Matches |
|---|---|
. |
Any one character (except a new line) |
\d \D |
A digit / a non-digit |
\w \W |
A letter, digit or underscore / anything else |
\s \S |
Whitespace / non-whitespace |
[abc] [a-z] [^0-9] |
One of a set / a range / anything except |
* |
Zero or more of the previous |
+ |
One or more |
? |
Zero or one (optional) |
{3} {2,4} |
Exactly 3 / between 2 and 4 |
^ $ |
Start / end of the text |
\b |
Edge of a word |
a|b |
a or b |
( ) |
A group |
import re
print(re.findall(r"[A-Z][a-z]+", "Sara met Ali in Lahore")) # capitalised words
print(re.findall(r"\d{3}-\d{4}", "Call 555-1234 or 555-9876")) # exact digit counts
print(re.findall(r"colou?r", "color colour colr")) # optional letter
print(re.findall(r"\bcat\b", "cat catalog concat cat")) # whole word only
print(re.findall(r"^\w+", "hello world")) # first word
['Sara', 'Ali', 'Lahore']
['555-1234', '555-9876']
['color', 'colour']
['cat', 'cat']
['hello']
Groups: pulling out the parts
import re
line = "2026-03-15 ERROR Database timed out"
m = re.search(r"(\d{4})-(\d{2})-(\d{2}) (\w+) (.*)", line)
print(m.groups())
year, month, day, level, message = m.groups()
print(level, "-", message)
# named groups make code self-explanatory
m = re.search(r"(?P<user>\w+)@(?P<domain>[\w.]+)", "Write to sara@example.com today")
print(m.group("user"), m.group("domain"))
print(m.groupdict())
('2026', '03', '15', 'ERROR', 'Database timed out')
ERROR - Database timed out
sara example.com
{'user': 'sara', 'domain': 'example.com'}
Real tasks: emails, phones, dates
import re
text = """Contact sara@example.com or ali.khan@company.co.pk.
Phones: 0300-1234567, +92 321 7654321.
Meeting on 15/03/2026 and again on 2026-04-01."""
emails = re.findall(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+", text)
print(emails)
phones = re.findall(r"(?:\+92 ?|0)3\d{2}[- ]?\d{7}", text)
print(phones)
dates = re.findall(r"\d{2}/\d{2}/\d{4}|\d{4}-\d{2}-\d{2}", text)
print(dates)
['sara@example.com', 'ali.khan@company.co.pk']
['0300-1234567', '+92 321 7654321']
['15/03/2026', '2026-04-01']
There is no single perfect email pattern: real email rules are enormously complicated. For forms, use a simple pattern like the one above and confirm the address by actually sending a message.
Replacing and splitting
import re
text = "Call me maybe, or don't"
print(re.sub(r"\s+", " ", text)) # collapse extra spaces
print(re.sub(r"\d", "#", "Card 1234 5678")) # mask digits
print(re.sub(r"(\w+)@(\w+)", r"\2 has \1", "sara@home")) # reuse groups
print(re.split(r"[,;]\s*", "a, b;c, d")) # split on several separators
print(re.sub(r"\b(\w)(\w*)", lambda m: m.group(1).upper() + m.group(2), "make title case"))
Call me maybe, or don't
Card #### ####
home has sara
['a', 'b', 'c', 'd']
Make Title Case
Greedy versus lazy
import re
html = "<b>bold</b> and <i>italic</i>"
print(re.findall(r"<.+>", html)) # greedy: grabs as much as possible
print(re.findall(r"<.+?>", html)) # lazy: grabs as little as possible
['<b>bold</b> and <i>italic</i>']
['<b>', '</b>', '<i>', '</i>']
Compile, flags and validating whole strings
import re
pattern = re.compile(r"^[A-Z]{2}\d{4}$") # compile once, reuse many times
for code in ["AB1234", "ab1234", "ABC123", "XY9999"]:
print(code, bool(pattern.fullmatch(code)))
print(re.findall(r"python", "Python PYTHON python", re.IGNORECASE))
multi = "line one\nline two\nline three"
print(re.findall(r"^line (\w+)$", multi, re.MULTILINE))
AB1234 True
ab1234 False
ABC123 False
XY9999 True
['Python', 'PYTHON', 'python']
['one', 'two', 'three']
Using regex for everything. If "hello" in text, startswith or split solves it, use that: simpler and clearer. Regex is for patterns with variety, not fixed text. Also avoid parsing complete HTML or JSON with regex; use a proper parser.
Forgetting the raw string. "\d" without the r prefix may work with a warning today and break later. Always write r"\d".
Extract all hashtags from "Learning #python and #coding today! #100DaysOfCode".
Show one possible answer
import re
text = "Learning #python and #coding today! #100DaysOfCode"
print(re.findall(r"#\w+", text))
['#python', '#coding', '#100DaysOfCode']
Validate a password: at least 8 characters with at least one digit and one uppercase letter. Test it on three examples.
Hint: (?=.*\d) means “somewhere ahead there is a digit”.
Show one possible answer
import re
def strong(pw):
return bool(re.fullmatch(r"(?=.*\d)(?=.*[A-Z]).{8,}", pw))
for pw in ["weakpass", "Strong123", "Short1A"]:
print(pw, strong(pw))
weakpass False
Strong123 True
Short1A False
Write a tiny pattern, test it, then extend it. Paste the pattern and sample text into an online tester such as regex101.com, which explains each part in plain language.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which prefix should regex patterns use in Python?
- f for format
- b for bytes
- r for raw string
- None
Raw strings keep backslashes intact.
What does \d+ match?
- One or more digits
- Exactly one digit
- A letter
- Whitespace
\d is a digit, + means one or more.
Which function returns every match as a list?
- re.search
- re.findall
- re.match
- re.sub
findall collects all matches.
What does a group in parentheses do?
- Repeats it
- Ignores it
- Deletes it
- Captures part of the match
Use groups() or group(n) to read them.
What does .+? do compared with .+?
- Matches nothing
- Matches as little as possible
- Matches more
- Is an error
The ? makes it lazy.
Summary
- Use raw strings for patterns.
- \d \w \s, [ ], * + ? { } build patterns.
- Groups capture pieces of a match.
- sub replaces; findall finds everything.
Questions
Why does my pattern match too much?
Quantifiers are greedy by default. Add ? to make them lazy, or make the pattern more specific.
What is the difference between match, search and fullmatch?
match checks the start, search looks anywhere, and fullmatch needs the whole string to fit.
Is regex hard to learn?
The syntax is dense, but ten building blocks cover most real tasks. Practise with small examples.
