Last modified: Nov 19, 2026
Python Find All Matches with re.findall
Finding every match inside a string is a common task in Python. The re.findall function makes it easy. It scans text and returns a list of all matches.
This guide explains how re.findall works. You will see clear examples with code and output. By the end, you will use it with confidence.
What Is re.findall?
The re.findall function is part of Python's built-in re module. It searches a string for a pattern. It returns all non-overlapping matches as a list.
If nothing matches, it returns an empty list. It never returns None. That makes it safe and simple to use.
If you are new to regular expressions, start with a solid overview of the Python regex re module guide. It covers the basics you need before using re.findall.
Basic Syntax
The syntax is short and clear.
import re
# re.findall(pattern, string, flags=0)
The pattern is the regular expression. The string is the text to search. The optional flags change how matching works.
Your First Example
Let's find all numbers in a sentence.
import re
text = "I have 2 apples and 15 oranges."
numbers = re.findall(r"\d+", text)
print(numbers)
['2', '15']
The pattern \d+ means one or more digits. The function returns every match as a string in a list.
Finding Words
You can match full words with the \w+ pattern.
import re
text = "Python is fun and Python is fast."
words = re.findall(r"\w+", text)
print(words)
['Python', 'is', 'fun', 'and', 'Python', 'is', 'fast']
Each word becomes an item in the list. Punctuation is skipped because \w matches letters, digits, and underscores only.
Case-Insensitive Matching
Use the re.IGNORECASE flag to ignore letter case.
import re
text = "Python PYTHON python"
matches = re.findall(r"python", text, re.IGNORECASE)
print(matches)
['Python', 'PYTHON', 'python']
All three forms match. The flag makes your pattern more flexible.
Using Groups with re.findall
Groups change the output. If your pattern has one group, the function returns a list of group contents, not full matches.
import re
text = "Name: Alice, Name: Bob"
names = re.findall(r"Name: (\w+)", text)
print(names)
['Alice', 'Bob']
With two or more groups, you get a list of tuples.
import re
text = "Alice:25, Bob:30"
pairs = re.findall(r"(\w+):(\d+)", text)
print(pairs)
[('Alice', '25'), ('Bob', '30')]
This behavior surprises many beginners. Remember it: groups shape the result.
Non-Capturing Groups
Sometimes you need grouping but not capturing. Use (?:...) to keep the full match in the result.
import re
text = "cat bat hat"
matches = re.findall(r"(?:c|b|h)at", text)
print(matches)
['cat', 'bat', 'hat']
The full words appear because the group does not capture. This tip keeps your output clean.
Common Use Cases
Extract emails from text.
import re
text = "Contact us at [email protected] or [email protected]"
emails = re.findall(r"[\w.]+@[\w.]+", text)
print(emails)
['[email protected]', '[email protected]']
Find all hashtags.
import re
post = "Loving #python and #regex today"
tags = re.findall(r"#\w+", post)
print(tags)
['#python', '#regex']
Pull numbers from mixed text. This is like scanning values before you find max and min in a list using Python. Extract first, then process.
import re
data = "scores: 88, 92, 75"
scores = [int(n) for n in re.findall(r"\d+", data)]
print(scores)
[88, 92, 75]
re.findall vs re.search and re.match
These functions serve different needs. Pick the right one for the job.
re.search returns the first match as a match object. re.match only checks the start of the string. re.findall returns every match as a list.
If you need positions or groups with details, use re.finditer. It returns match objects you can loop over.
import re
text = "abc 123 def 456"
for m in re.finditer(r"\d+", text):
print(m.group(), m.start())
123 4
456 12
Use re.findall when you only need the matched text. Use re.finditer when you need extra details.
Handling Overlapping Matches
re.findall does not return overlapping matches. Each match moves the search forward.
import re
text = "aaa"
print(re.findall(r"aa", text))
['aa']
Only one match appears. If you need overlaps, use lookahead patterns.
Performance Tips
Compile patterns that you reuse many times. This saves time on large text.
import re
pattern = re.compile(r"\d+")
print(pattern.findall("a1 b22 c333"))
['1', '22', '333']
Keep patterns simple. Avoid greedy wildcards when possible. Test on small samples first.
Regex skill pairs well with other basics. For example, you can build small tools like a Python simple calculator program once you can parse input text.
Practical Mini Project
Let's extract dates from a log file.
import re
log = """
2023-01-15: user login
2023-02-20: user logout
2023-03-05: user login
"""
dates = re.findall(r"\d{4}-\d{2}-\d{2}", log)
print(dates)
['2023-01-15', '2023-02-20', '2023-03-05']
The pattern matches the year, month, and day format. Each date becomes a list item.
Small parsing tasks like this appear in many programs. The same thinking helps with logic tasks, such as a Python leap year program with examples or converting values like in a Python convert Celsius to Fahrenheit script.
Common Mistakes to Avoid
Forgetting raw strings. Always use r"..." for patterns. It prevents escape character bugs.
Ignoring group behavior. Capturing groups change the output shape. Plan for it.
Expecting None.re.findall returns an empty list, never None. Check for emptiness with if not matches.
Mixing up functions. Do not use re.search when you need all matches. Use re.findall instead.
More Practice Ideas
Try these exercises to build skill. Extract phone numbers. Pull out URLs. Find all capital words. Count repeated words.
Each exercise teaches pattern design. Start simple and grow. Regular expressions reward practice.
You can also practice with logic problems. For example, use Python to check if a number is even or odd, or generate a Python Fibonacci sequence with examples, and combine them with regex input parsing.
Other fun drills include a Python factorial program with examples or a Python prime number check program. These build core skills that support text processing too.
For numeric output, you may want clean formatting. Learn how to do Python round to 2 decimal places when you print results from parsed data.
Conclusion
re.findall is a powerful and simple tool. It returns a list of every match in a string. It handles patterns, flags, and groups with ease.
Remember the key rules. Use raw strings. Watch for group behavior. Expect a list, not None. Choose the right regex function for the task.
Start with small examples. Test each pattern. With practice, you will extract data from text quickly and reliably. Happy coding!