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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:10:01+00:00 2026-06-11T15:10:01+00:00

I have a question about regular expression sub in python. So, I have some

  • 0

I have a question about regular expression sub in python. So, I have some lines of code and what I want is to replace all floating point values eg: 2.0f,-1.0f…etc..to doubles 2.0,-1.0. I came up with this regular expression '[-+]?[0-9]*\.?[0-9]+f' and it finds what I need but I am not sure how to replace it?

so here’s what I have:

# check if floating point value exists
if re.findall('[-+]?[0-9]*\.?[0-9]+f', line):
    line = re.sub('[-+]?[0-9]*\.?[0-9]+f', ????? ,line)

I am not sure what to put under ????? such that it will replace what I found in '[-+]?[0-9]*\.?[0-9]+f' without the char f in the end of the string.

Also there might be more than one floating point values, which is why I used re.findall

Any help would be great. Thanks

  • 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-06-11T15:10:02+00:00Added an answer on June 11, 2026 at 3:10 pm

    Capture the part of the text you want to save in a capturing group and use the \1 substitution operator:

    line = re.sub(r'([-+]?[0-9]*\.?[0-9]+)f', r'\1' ,line)
    

    Note that findall (or any kind of searching) is unnecessary since re.sub will look for the pattern itself and return the string unchanged if there are no matches.

    Now, for several regular expression writing tips:

    • Always use raw strings (r'...') for regular expressions and substitution strings, otherwise you will need to double your backslashes to escape them from Python’s string parser. It is only by accident that you didn’t need to do this for \., since . is not part of an escape sequence in Python strings.

    • Use \d instead of [0-9] to match a digit. They are equivalent, but \d is easier to recognize for “digit”, while [0-9] needs to be visually verified.

    • Your regular expression will not recognize 10.f, which is likely a valid decimal number in your input. Matching floating-point numbers in various formats is trickier than it seems at first, but simple googling will reveal many reasonably complete solutions for this.

    • The re.X flag will allow you to add arbitrary whitespace and even comments to your regexp. With small regexps that can seem downright silly, but for large expressions the added clarity is a life-saver. (Your regular expression is close to the threshold.)

    Here is an example of an extended regular expression that implements the above style tips:

    line = re.sub(r'''
        ( [-+]?
          (?: \d+ (?: \.\d* )?    # 12 or 12. or 12.34
              |
              \.\d+               # .12
          )
        ) f''',
        r'\1', line, flags=re.X)
    

    ((?:...) is a non-capturing group, only used for precedence.)

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

Sidebar

Related Questions

I have a question about the following regular expression: I want to match the
Hello I have a question about matching groups based on the following regular expression
I have a quite general question about java and regular expression. If we lock
I have a simple question about finding a regular expression for a given language.
Suppose we have some text and a regular expression that matches it. Question: if
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all
I have question about interpreting strings as packed binary data in C++. In python,
I have a question about how GC works in Java. Consider the following code:
I have a question about SqlDataReader: Is there some way to modificated the values

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.