How can I find all matches to a regular expression in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, the usual way to find every regex match is to use the re module. The right function depends on what you need back: strings, match objects, capture groups, or overlapping matches. Once you understand the return shape of each function, the task becomes predictable instead of trial and error.
Use re.findall For The Simplest Case
If you only need the matched text, re.findall is usually the fastest path.
This returns:
findall scans left to right and returns non-overlapping matches. That makes it ideal for extraction tasks where positions and metadata are not needed.
Use re.finditer When You Need Positions
If you need start and end indexes, use re.finditer. It yields match objects instead of plain strings.
This is better than findall when you want to highlight matches in a UI, replace selected ranges, or build a parser.
Capturing Groups Change findall Output
One common source of confusion is that findall changes its return value when the pattern contains capturing groups.
The result is:
That behavior is useful once you know it, but surprising if you expected full matched strings. If you want the whole match and grouped parts, finditer is often clearer.
Compile The Pattern For Reuse
If the same expression is used repeatedly, compile it once.
Compiled patterns improve readability and avoid repeating flags in every call.
Handle Overlapping Matches With Lookahead
findall does not return overlapping matches. If you need overlaps, use a zero-width lookahead.
This returns two matches for aba, starting at different positions. Without the lookahead, Python would return only the first non-overlapping match.
Flags Matter More Than People Expect
Many "why did regex miss this?" bugs are really flag problems.
You may also need re.MULTILINE for line-anchored patterns or re.DOTALL if . should cross newline boundaries.
A Practical Extraction Example
Here is a small example that extracts emails from text and preserves where they occurred.
This is the sort of task where finditer is usually better than findall, because position data is part of the result you care about.
Common Pitfalls
- Using
findalland forgetting that capturing groups change the return type. - Expecting overlapping matches without using a lookahead-based pattern.
- Using
findallwhen you really need positions and match metadata. - Forgetting flags such as
re.IGNORECASEorre.MULTILINE. - Writing patterns that are too broad and then blaming the matching function instead of the regex itself.
Summary
- Use
re.findallwhen you want all matched strings quickly. - Use
re.finditerwhen you need match objects and positions. - Remember that capturing groups change what
findallreturns. - Use compiled patterns when the same regex appears in multiple places.
- For overlapping matches, switch to a lookahead-based pattern instead of expecting
findallto do it automatically.
Related reading
- How can I find script's directory?
- How can I find the current OS in Python?
- How can I find the first occurrence of a sub-string in a python string?
- How can I find the index for a given item in a list?
- How can I find the number of arguments of a Python function?
- How can I find where Python is installed on Windows?
- How can I fix the zsh command not found python error? macOS Monterey 12.3, Pythonnbsp;3.10, Atom IDE, and atom-python-run 0.9.7
- How can I fix UnboundLocalError local variable referenced before assignment?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.