re Module: search, match, findall, sub
re.search(), re.match(), re.findall(), re.sub(), re.compile() for performance.
re Module: search, match, findall, sub is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The re Module
Python exposes regex through the built-in re module. The core functions are search, match, findall, and sub.
Knowing which to reach for is half the battle.
import re # always import it firstre.search — Find Anywhere
re.search(pattern, text) scans the whole string for the first match and returns a Match object, or None if nothing matches.
import re
m = re.search("\d+", "order 42 shipped")
if m:
print(m.group()) # "42"
print(m.start()) # 6re.match — Anchored at the Start
re.match only matches at the beginning of the string. If the pattern is not at position 0, it returns None even if it appears later.
import re
print(re.match("\d+", "42 items")) # matches "42"
print(re.match("\d+", "items 42")) # Nonesearch vs match
The key difference:
match— pattern must start at index 0search— pattern may appear anywhere
Most of the time you want search. Use match only when you specifically need a prefix check.
re.findall — Get Every Match
re.findall returns a list of all non-overlapping matches as strings (not Match objects). Ideal for extraction tasks.
import re
emails = re.findall("\w+@\w+\.\w+", "a@x.com and b@y.org")
print(emails) # ["a@x.com", "b@y.org"]findall with Groups
If your pattern has capturing groups, findall returns the captured parts instead of the whole match — a common surprise.
Here each tuple holds the area code and the rest.
import re
print(re.findall("(\d{3})-(\d{4})", "555-1234 and 999-8765"))
# [("555", "1234"), ("999", "8765")]re.finditer — Match Objects
When you need positions or groups for every match, use re.finditer. It yields Match objects you can iterate over lazily.
import re
for m in re.finditer("\d+", "a1 b22 c333"):
print(m.group(), "at", m.start())re.sub — Search and Replace
re.sub(pattern, replacement, text) replaces every match with the replacement string and returns a new string.
Foundational for text cleaning — masking, normalizing, stripping.
import re
clean = re.sub("\d+", "#", "Room 101 and 202")
print(clean) # "Room # and #"sub with Backreferences and Functions
The replacement can reference captured groups with \1, or be a function for dynamic logic.
import re
# Swap "first last" -> "last, first"
print(re.sub("(\w+) (\w+)", "\\2, \\1", "Ada Lovelace"))
# Function replacement: double each number
print(re.sub("\d+", lambda m: str(int(m.group()) * 2), "a1 b2"))re.compile — Reuse Patterns
When you use a pattern many times (e.g. in a loop), re.compile builds it once into a reusable pattern object. This is faster and reads cleanly.
import re
num = re.compile("\d+")
for line in ["a1", "b22", "c333"]:
print(num.findall(line))Useful Flags
Flags change matching behavior:
re.IGNORECASE— case-insensitivere.MULTILINE—^/$match each linere.DOTALL—.also matches newlines
import re
print(re.findall("cat", "Cat CAT cat", re.IGNORECASE))
# ["Cat", "CAT", "cat"]Quick Check: search vs findall
You need a list of every phone number that appears anywhere in a document.
Recap: The re Module
You now know the core regex functions:
re.search— first match anywhere (Match object)re.match— match only at the startre.findall— list of all matches (groups change the output)re.finditer— Match objects with positionsre.sub— replace matches, with backrefs or a functionre.compileand flags for reuse and control
Next: capturing groups and named groups.
Frequently asked questions
Is the “re Module: search, match, findall, sub” lesson free?
Yes — the full text of “re Module: search, match, findall, sub” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “re Module: search, match, findall, sub”?
re.search(), re.match(), re.findall(), re.sub(), re.compile() for performance. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “re Module: search, match, findall, sub” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Regex Patterns and Character Classes
- re Module: search, match, findall, sub
- Capturing Groups and Named Groups
- Text Cleaning for AI with Regex