Possible Duplicate:
How to detect a floating point number using a regular expression
How do I write a Python regular expression that matches string representations of floating point numbers?
The expression should match any string that is accepted by the float
constructor as in float('3.5')
. Thus the expression should match '0.'
and '.0'
but not '.'
There is no need to match string representations of infinity and NaN.
[0-9]+|[0-9]+). This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).
To extract a floating number from a string with Python, we call the re. findall method. import re d = re. findall("\d+\.
Introduction to the Python regex backreferences The backreferences allow you to reference capturing groups within a regular expression. In this syntax, N can be 1, 2, 3, etc. that represents the corresponding capturing group. Note that the \g<0> refer to the entire match, which has the same value as the match.
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.
r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With