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)
Code language: Python (python)
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
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.
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"
Code language: Python (python)
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
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
Regex to find all words containing a certain letter
In this example, we will see how to find words that contain the letter ‘i’.
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
Previous:
Next:
Leave a Reply