How to write a Python regular expression that matches floating point numbers?

PythonServer Side ProgrammingProgramming

The following code uses Python regex to match floating point numbers

Example

import re
s = '234.6789'
match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',s)
print match.group()
s2 = '0.45'
match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',s2)
print match.group()

Output

This gives the output

234.6789
0.45
raja
Published on 10-Jan-2018 18:09:49
Advertisements