1. Introduction
Regular expressions (regex) are patterns used to search, match, validate, and extract text. Python's built-in re module implements a full regex engine, letting you check whether a string matches a pattern, pull out substrings, split text on complex delimiters, or perform pattern-based find-and-replace -- far more powerful than plain string methods like find() or replace() for anything beyond exact substrings.
Cricket analogy: Regex is like a scout's detailed checklist for spotting a specific bowling action across hours of footage -- far more powerful than simply skimming for a player's name (find()), letting you extract every no-ball pattern at once.
2. Syntax
import re
pattern = r"\d+"
result = re.findall(pattern, "Order 42 has 7 items")
print(result)3. Explanation
re.match() only checks for a match at the beginning of the string; re.search() scans the whole string and returns the first match anywhere; re.fullmatch() requires the entire string to match the pattern exactly. Quantifiers can be greedy (*, +, {m,n}) which match as much text as possible, or non-greedy/lazy (*?, +?, {m,n}?) which match as little as possible.
Cricket analogy: re.match() is like checking only the very first ball of an over for a no-ball, re.search() scans the whole over for any no-ball, and re.fullmatch() demands every ball in the over be a no-ball; a greedy quantifier is like a batsman going for every run possible, while lazy takes just a single run.
Common metacharacters include \d (digit), \w (word character: letters, digits, underscore), \s (whitespace), . (any character except newline), ^/$ (start/end of string), [] (character class), () (grouping/capturing), and | (alternation). re.findall() returns all non-overlapping matches as a list, re.sub() performs pattern-based replacement, and re.compile() pre-compiles a pattern into a reusable Pattern object for efficiency when the same regex is used repeatedly.
Cricket analogy: \d is like matching only jersey numbers, \w matches any player-name character, \s matches gaps between names on a team sheet, and () groups a player's stats together; re.findall() lists every six hit in an innings, re.sub() replaces a player's old name in the record book, and re.compile() pre-builds a reusable scouting filter for the whole season.
Always write regex patterns as raw strings (e.g., r"\d+" instead of "\\d+") to avoid Python's own string-escaping rules conflicting with the regex engine's escaping rules. Without the r prefix, backslashes in patterns like \d or \s can be misinterpreted or require doubling up.
4. Example
import re
text = "Contact: john.doe@example.com or jane_smith99@test.org"
pattern = r"[\w.]+@[\w.]+"
matches = re.findall(pattern, text)
print(matches)
m = re.search(r"(\w+)@(\w+)", text)
print(m.group(1))
print(m.group(2))5. Output
['john.doe@example.com', 'jane_smith99@test.org']
doe
example6. Key Takeaways
- re.match checks only the start of a string; re.search scans the whole string; re.fullmatch requires the entire string to match.
- Always use raw strings (r"...") for regex patterns to avoid escaping headaches.
- Greedy quantifiers (*, +) match as much as possible; adding ? (*?, +?) makes them non-greedy.
- re.findall returns a list of all matches; re.sub performs pattern-based substitution.
- re.compile() pre-compiles a pattern for reuse and better performance in loops.
- Common classes: \d digits, \w word characters, \s whitespace, . any character except newline.
Practice what you learned
1. What is the key difference between re.match() and re.search()?
2. Why should regex patterns be written as raw strings in Python?
3. What does the quantifier `.*?` mean compared to `.*`?
4. What does re.findall() return?
5. What is the result of re.sub(r"\s+", "-", "a b c")?
6. What does \w match in Python regex by default?
Was this page helpful?
You May Also Like
Strings in Python
Python's immutable text sequence type, covering string creation, indexing, slicing, immutability, and formatting.
Common String Methods in Python
A practical tour of Python's built-in string methods, grouped by case conversion, search/replace, and split/join, with usage examples.
Exceptions in Python
What exceptions are, how Python's built-in exception hierarchy is structured, and how errors propagate through a program.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics