In this article, we will learn how to find all matches to the regular expression in Python. The RE module’s re.findall()
method scans the regex pattern through the entire target string and returns all the matches that were found in the form of a list.
Table of contents
How to use re.findall()
Before moving further, let’s see the syntax of the re.findall()
method.
Syntax:
re.findall(pattern, string, flags=0)
pattern
: regular expression pattern we want to find in the string or textstring
: It is the variable pointing to the target string (In which we want to look for occurrences of the pattern).Flags
: It refers to optional regex flags. by default, no flags are applied. For example, there.I
flag is used for performing case-insensitive findings.
The regular expression pattern and target string are the mandatory arguments, and flags are optional.
Return Value
The re.findall()
scans the target string from left to right as per the regular expression pattern and returns all matches in the order they were found.
It returns None
if it fails to locate the occurrences of the pattern or such a pattern doesn’t exist in a target string.

Example to find all matches to a regex pattern
In this example, we will find all numbers present inside the target string. To achieve this, let’s write a regex pattern.
Pattern: \d+
What does this pattern mean?
- The
\d
is a special regex sequence that matches any digit from 0 to 9 in a target string. - The
+
metacharacter indicates number can contain at minimum one or maximum any number of digits.
In simple words, it means to match any number inside the following target string.
target_string = "Emma is a basketball player who was born on June 17, 1993. She played 112 matches with scoring average 26.12 points per game. Her weight is 51 kg."
As we can see in the above string ’17’, ‘1993’, ‘112’, ’26’, ’12’, ’51’ number are present, so we should get all those numbers in the output.
Example
import re
target_string = "Emma is a basketball player who was born on June 17, 1993. She played 112 matches with scoring average 26.12 points per game. Her weight is 51 kg."
result = re.findall(r"\d+", target_string)
# print all matches
print("Found following matches")
print(result)
# Output ['17', '1993', '112', '26', '12', '51']
Note:
First of all, I used a raw string to specify the regular expression pattern i.e r"\d+"
. As you may already know, the backslash has a special meaning in some cases because it may indicate an escape character or escape sequence to avoid that we must use raw string.
Finditer method
The re.finditer()
works exactly the same as the re.findall()
method except it returns an iterator yielding match objects matching the regex pattern in a string instead of a list.
It scans the string from left to right, and matches are returned in the iterator form. Later, we can use this iterator object to extract all matches.
In simple words, finditer()
returns an iterator over MatchObject objects.

But why use finditer()
?
In some scenarios, the number of matches is high, and you could risk filling up your memory by loading them all using findall()
. Instead of that using the finditer()
, you can get all possible matches in the form of an iterator object, which will improve performance.
It means, finditer()
returns a callable object which will load results in memory when called. Please refer to this Stackoverflow answer to get to know the performance benefits of iterators.
finditer example
Now, Let’s see the example to find all two consecutive digits inside the target string.
import re
target_string = "Emma is a basketball player who was born on June 17, 1993. She played 112 matches with a scoring average of 26.12 points per game. Her weight is 51 kg."
# finditer() with regex pattern and target string
# \d{2} to match two consecutive digits
result = re.finditer(r"\d{2}", target_string)
# print all match object
for match_obj in result:
# print each re.Match object
print(match_obj)
# extract each matching number
print(match_obj.group())
Output:
re.Match object; span=(49, 51), match='17' 17 re.Match object; span=(53, 55), match='19' 19 re.Match object; span=(55, 57), match='93' 93 re.Match object; span=(70, 72), match='11' 11 re.Match object; span=(103, 105), match='26' 26 re.Match object; span=(106, 108), match='12' 12 re.Match object; span=(140, 142), match='51' 51
More use
- Use finditer to find the indexes of all regex matches
- Regex findall special symbols from a string
Regex find all word starting with specific letters
In this example, we will see solve following 2 scenarios
- find all words that start with a specific letter/character
- find all words that start with a specific substring
Now, let’s assume you have the following string:
target_string = "Jessa is a Python developer. She also gives Python programming training"
Now let’s find all word that starts with letter p. Also, find all words that start with substring ‘py‘
Pattern: \b[p]\w+\b
- The
\b
is a word boundary, then p in square bracket[]
means the word must start with the letter ‘p‘. - Next,
\w+
means one or more alphanumerical characters after a letter ‘p’ - In the end, we used
\b
to indicate word boundary i.e. end of the word.
Example
import re
target_string = "Jessa is a Python developer. She also gives Python programming training"
# all word starts with letter 'p'
print(re.findall(r'\b[p]\w+\b', target_string, re.I))
# output ['Python', 'Python', 'programming']
# all word starts with substring 'Py'
print(re.findall(r'\bpy\w+\b', target_string, re.I))
# output ['Python', 'Python']
Regex to find all word that starts and ends with a specific letter
In this example, we will see solve following 2 scenarios
- find all words that start and ends with a specific letter
- find all words that start and ends with a specific substring
Example
import re
target_string = "Jessa is a Python developer. She also gives Python programming training"
# all word starts with letter 'p' and ends with letter 'g'
print(re.findall(r'\b[p]\w+[g]\b', target_string, re.I))
# output 'programming'
# all word starts with letter 'p' or 't' and ends with letter 'g'
print(re.findall(r'\b[pt]\w+[g]\b', target_string, re.I))
# output ['programming', 'training']
target_string = "Jessa loves mango and orange"
# all word starts with substring 'ma' and ends with substring 'go'
print(re.findall(r'\bma\w+go\b', target_string, re.I))
# output 'mango'
target_string = "Kelly loves banana and apple"
# all word starts or ends with letter 'a'
print(re.findall(r'\b[a]\w+\b|\w+[a]\b', target_string, re.I))
# output ['banana', 'and', 'apple']
Regex to find all words containing a certain letter
In this example, we will see how to find words that contain the letter ‘i’.
import re
target_string = "Jessa is a knows testing and machine learning"
# find all word that contain letter 'i'
print(re.findall(r'\b\w*[i]\w*\b', target_string, re.I))
# found ['is', 'testing', 'machine', 'learning']
# find all word which contain substring 'ing'
print(re.findall(r'\b\w*ing\w*\b', target_string, re.I))
# found ['testing', 'learning']
Regex findall repeated characters
For example, you have a string: ""Jessa Erriika""
As the result you want to have the following matches: (J, e, ss, a, E, rr, ii, k, a)
Example
import re
target_string = "Jessa Erriika"
# This '\w' matches any single character
# and then its repetitions (\1*) if any.
matcher = re.compile(r"(\w)\1*")
for match in matcher.finditer(target_string):
print(match.group(), end=", ")
# output J, e, ss, a, E, rr, ii, k, a,
Previous:
Next:
Leave a Reply