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 7537019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:44:02+00:00 2026-05-30T06:44:02+00:00

I have a python function which uses a regular expression to match one (the

  • 0

I have a python function which uses a regular expression to match one (the first) floating point number in a given string and return the number.

How can I modify it (the regex) to generalize the function so that it returns instead a list with the all numbers in the string?

Here is a working demonstration:

    import re

    def extract_number(s,notfound='NOT_FOUND'):
        regex='[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?' # matching any floating point number 
        m = re.search(regex,s)
        if(m): val=m.group()
        else: val=notfound
        return val

    example='bla1.23bar4.5fuzz6.7cat8'

    print example
    print extract_number(example)

In this example, the output is:

bla1.23bar4.5fuzz6.7cat8
1.23

The modified function which I am looking for, let’s call it extract_numbers (note the plural !), should output this:

bla1.23bar4.5fuzz6.7cat8
[1.23, 4.5, 6.7, 8]

  • 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-30T06:44:04+00:00Added an answer on May 30, 2026 at 6:44 am

    Just use re.findall:

    def extract_number(s,notfound='NOT_FOUND'):
        regex=r'[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?'
        return re.findall(regex,s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python function in which I am doing some sanitisation of the
I have two Python functions, both of which take variable arguments in their function
I have been wondering about the reload() function in python, which seems like it
I have a c99 function that uses openmp, which works as expected. I also
I have written a small function, which uses ElementTree and xpath to extract the
Sometimes checking of arguments in Python is necessary. e.g. I have a function which
Well, I have created a python script, which checks the number of uncompleted tasks
I have a Python function which generates an image once it is accessed. I
i am working on a regex match function in python. i have the following
I have various python function which i intend to run independently. For exmaple ,

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.