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:
Construct the Regexp:
import re
pattr = re.compile(???)
x = pattr.match("4.5")
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
$""")
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
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']
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