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


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']

Updated on: 02-Nov-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements