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


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

Updated on: 20-Feb-2020

742 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements