"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can Regular Expressions Be Used to Extract Floating-Point Values from Strings?

How Can Regular Expressions Be Used to Extract Floating-Point Values from Strings?

Published on 2024-11-09
Browse:779

How Can Regular Expressions Be Used to Extract Floating-Point Values from Strings?

Extracting Floating-Point Values from Strings with Regular Expressions

Consider the task of extracting a double value from a string. To achieve this using a regular expression, the following steps are involved:

  1. Construct the Regexp:

    import re
    
    pattr = re.compile(???)
    x = pattr.match("4.5")
  2. Use Perl-Compatible Regular Expressions:
    A suitable regexp from the Perl documentation for extracting floating-point values is:

    re_float = re.compile("""(?x)
    ^
       [ -]?\ *      # an optional sign and space
       (             # integers or f.p. mantissas
           \d        # start with a ...
           (           # ? takes care of integers
               \.\d* # mantissa a.b or a.
           )?
          |\.\d      # mantissa .b
       )
       ([eE][ -]?\d )?  # optionally match an exponent
    $""")
  3. Find and Retrieve Matches:
    To extract the double value, apply the compiled regexp to the desired string:

    m = re_float.match("4.5")
    print(m.group(0))

    This will output:

    4.5
  4. Extract Multiple Values from a String:
    To extract multiple floating-point values from a larger string, use the findall() method:

    s = """4.5 abc -4.5 abc - 4.5 abc   .1e10 abc . abc 1.01e-2 abc 
        1.01e-.2 abc 123 abc .123"""
    print(re.findall(r"[ -]? *(?:\d (?:\.\d*)?|\.\d )(?:[eE][ -]?\d )?", s))

    This will return a list of extracted values, including:

    ['4.5', '-4.5', '- 4.5', '  .1e10', ' 1.01e-2', '       1.01', '-.2', ' 123', ' .123']
Release Statement This article is reprinted at: 1729482499 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3