Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:05:39+00:00 2026-05-26T19:05:39+00:00

I need to find all matches in a string for a given regex. I’ve

  • 0

I need to find all matches in a string for a given regex. I’ve been using findall() to do that until I came across a case where it wasn’t doing what I expected. For example:

regex = re.compile('(\d+,?)+')
s = 'There are 9,000,000 bicycles in Beijing.'

print re.search(regex, s).group(0)
> 9,000,000

print re.findall(regex, s)
> ['000']

In this case search() returns what I need (the longest match) but findall() behaves differently, although the docs imply it should be the same:

findall() matches all occurrences of a pattern, not just the first one
as search() does.

  • Why is the behaviour different?

  • How can I achieve the result of search() with findall() (or something else)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:05:40+00:00Added an answer on May 26, 2026 at 7:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find all the regex matches from a list of strings. For
I've been trying to find a string in PHP that matches something like this:
I need to find all descendants of a category using HierarchyID for SQL Server
Let's say i need to find all .bar elements inside an element that's assigned
I have a pdf file.I need to find all the hyperlinks available in that
In the quest for localization I need to find all the string literals littered
I have a pattern @@{} and given a string I need to find out
I have an application where I need to parse a string to find all
I need help with a regular expression that will find matches in the strings
I need to find all occurrences of a function call in a C++ file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.