re Module: search, match, findall, sub
re.search(), re.match(), re.findall(), re.sub(), re.compile() for performance.
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()) # 6All 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