Use re.findall or re.finditer instead.

re.findall(pattern, string) returns a list of matching strings.

re.finditer(pattern, string) returns an iterator over MatchObject objects.

Example:

re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')
# Output: ['cats', 'dogs']

[x.group() for x in re.finditer( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')]
# Output: ['all cats are', 'all dogs are']
Answer from Amber on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.7 ...
Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
If you are unsure if a character has special meaning, such as '@', you can try putting a slash in front of it, \@. If its not a valid escape sequence, like \c, your python program will halt with an error. Joke: what do you call a pig with three eyes? piiig! The basic rules of regular expression search for a pattern within a string are: The search proceeds through the string from start to end, stopping at the first match found · All of the pattern must be matched, but not all of the string
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-search-in-python
re.search() in Python - GeeksforGeeks
June 18, 2026 - Example 3: In this example, we want to check whether the given string begins with a capital letter (A–Z). We're using the re.search() function along with a regular expression pattern that looks for a capital letter at the beginning of the string. ... import re s = "Python is great" # match capital letter at start pat = r"^[A-Z]" # search pattern res = re.search(pat, s) if res: print(res.group()) else: print("No")
🌐
PYnative
pynative.com › home › python › regex › python regex find all matches using findall() and finditer()
Python Regex Find All Matches using findall() and finditer()
July 27, 2021 - Find all matches to the regular expression in Python using findall() and finditer(). Scans the regex pattern and returns all the matches that were found
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-findall-in-python
re.findall() in Python - GeeksforGeeks
July 23, 2025 - re.findall() method in Python helps us find all pattern occurrences in a string. It's like searching through a sentence to find every word that matches a specific rule.
🌐
PYnative
pynative.com › home › python › regex › python regex search using re.search()
Python Regex Search using re.search()
April 2, 2021 - Python regex re.search() method looks for occurrences of the regex pattern inside the entire target string and returns the corresponding Match Object instance where the match found.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-regex-re-search-vs-re-findall
Python Regex: re.search() VS re.findall() - GeeksforGeeks
July 12, 2025 - Two commonly used functions are re.search(), which finds the first occurrence of a pattern in a string and re.findall(), which retrieves all matches of a pattern throughout the string.
Find elsewhere
🌐
Guru99
guru99.com › home › python › python regex: re.match(), re.search(), re.findall() with example
Python RegEx: re.match(), re.search(), re.findall() with Example
June 23, 2026 - For example here we look for two ... hence it returns the output as “No match”. findall() module is used to search for “all” occurrences that match a given pattern....
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
A special sequence is a \ followed ... inside a pair of square brackets [] with a special meaning: The findall() function returns a list containing all matches....
🌐
GeeksforGeeks
geeksforgeeks.org › how-can-i-find-all-matches-to-a-regular-expression-in-python
How Can I Find All Matches to a Regular Expression in Python? - GeeksforGeeks
August 30, 2024 - This function returns a list of all non-overlapping matches of the pattern in the string. The re.findall() function takes two main arguments. The first is the regex pattern you want to search for and the string where you want to perform the search.
🌐
Medium
medium.com › @ynagalakshmi89 › getting-started-with-regex-in-python-basics-and-re-findall-explained-95d3ed6ab026
“Getting Started with Regex in Python: Basics and re.findall Explained” | by Nagalakshmi Yadlapalli | Medium
December 30, 2025 - re.findall() method in Python helps us find all pattern occurrences in a string. It’s like searching through a sentence to find every word that matches a specific rule.
🌐
AppSignal
blog.appsignal.com › home › python › python regex: how to use re.search, re.match, and re.findall
Python Regex: How to Use re.search, re.match, and re.findall | AppSignal Blog
January 15, 2025 - The re.search() function searches for the first match of the regex pattern in a string. Suppose, for example, that you want to extract any numbers in a string. We could write the following Python code:
Top answer
1 of 2
20

Ok, I see what's going on... from the docs:

If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

As it turns out, you do have a group, "(\d+,?)"... so, what it's returning is the last occurrence of this group, or 000.

One solution is to surround the entire regex by a group, like this

regex = re.compile('((\d+,?)+)')

then, it will return [('9,000,000', '000')], which is a tuple containing both matched groups. of course, you only care about the first one.

Personally, i would use the following regex

regex = re.compile('((\d+,)*\d+)')

to avoid matching stuff like " this is a bad number 9,123,"

Edit.

Here's a way to avoid having to surround the expression by parenthesis or deal with tuples

s = "..."
regex = re.compile('(\d+,?)+')
it = re.finditer(regex, s)

for match in it:
  print match.group(0)

finditer returns an iterator that you can use to access all the matches found. these match objects are the same that re.search returns, so group(0) returns the result you expect.

2 of 2
7

@aleph_null's answer correctly explains what's causing your problem, but I think I have a better solution. Use this regex:

regex = re.compile(r'\d+(?:,\d+)*')

Some reasons why it's better:

  1. (?:...) is a non-capturing group, so you only get the one result for each match.

  2. \d+(?:,\d+)* is a better regex, more efficient and less likely to return false positives.

  3. You should always use Python's raw strings for regexes if possible; you're less likely to be surprised by regex escape sequences (like \b for word boundary) being interpreted as string-literal escape sequences (like \b for backspace).

🌐
Note.nkmk.me
note.nkmk.me › home › python
Regular Expressions in Python: the re Module | note.nkmk.me
May 9, 2023 - All functions such as re.xxx() described below are also provided as methods of regex objects. It is more efficient to create and reuse a regex object when repeatedly performing the same pattern-based processing. In the following sample code, the function is used without compiling, but if the same pattern is used repeatedly, it is recommended to pre-compile and execute as a regex object method. Functions like match() and search() return a match object.
🌐
Finxter
blog.finxter.com › home › learn python blog › python re.findall() – everything you need to know
Python re.findall() - Everything You Need to Know - Be on the Right Side of Change
December 30, 2020 - The string 'Python is superior to Python' contains two occurrences of 'Python'. The search() method only returns a match object of the first occurrence. The findall() method returns a list of all occurrences.
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex findall()
Python Regex findall() Function By Practical Examples
December 10, 2021 - re.findall(pattern, string, flags=0)Code language: Python (python) ... The findall() function scans the string from left to right and finds all the matches of the pattern in the string.
🌐
Real Python
realpython.com › regex-python
Regular Expressions: Regexes in Python (Part 1) – Real Python
May 21, 2026 - Python, Java, and Perl all support regex functionality, as do most Unix tools and many text editors. Regex functionality in Python resides in a module named re. The re module contains many useful functions and methods, most of which you’ll learn about in the next tutorial in this series. For now, you’ll focus predominantly on one function, re.search().
🌐
Python documentation
docs.python.org › 3 › howto › regex.html
Regular expression HOWTO — Python 3.14.7 documentation
The re module provides an interface to the regular expression engine, allowing you to compile REs into objects and then perform matches with them. Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions.