PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » RegEx » Python Regex Find All Matches using findall() and finditer()

Python Regex Find All Matches using findall() and finditer()

Updated on: July 27, 2021 | Leave a Comment

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()
  • Example to find all matches to a regex pattern
  • Finditer method
    • finditer example
  • Regex find all word starting with specific letters
  • Regex to find all word that starts and ends with a specific letter
  • Regex to find all words containing a certain letter
  • Regex findall repeated characters

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)
  1. pattern: regular expression pattern we want to find in the string or text
  2. string: It is the variable pointing to the target string (In which we want to look for occurrences of the pattern).
  3. Flags: It refers to optional regex flags. by default, no flags are applied. For example, the re.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.

python regex findall
regex findall

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.

Python regex finditer
regex finditer()

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

  1. find all words that start with a specific letter/character
  2. 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

  1. find all words that start and ends with a specific letter
  2. 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:

Python Regex Search

Next:

Python Regex Split

Filed Under: Python, Python RegEx

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python RegEx

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

Posted In

Python Python RegEx
TweetF  sharein  shareP  Pin

  Python RegEx

  • Python RegEx
  • Python regex compile
  • Python regex match
  • Python regex search
  • Python regex findall
  • Python regex split
  • Python regex replace
  • Python regex capturing groups
  • Regex Metacharacters
  • Regex special sequences
  • Regex Flags

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com