How to extract floating number from text using Python regular expression?

PythonServer Side ProgrammingProgramming

The following code extracts floating numbers from given text/string using Python regex.

Example

import re
s = "Sound Level: -11.7 db or 15.2 or 8 db"
result = re.findall(r"[-+]?\d*\.\d+|\d+", s)
print result

Output

This gives the output

['-11.7', '15.2', '8']
raja
Published on 11-Jan-2018 12:57:39
Advertisements