
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract Floating Number from Text using Python Regular Expression
Extracting floating numbers from text/string can be done using Python's regular expressions which provides a flexible and effective way to parse numerical data embedded in strings. Regex can be customized to extract floating-point numbers, whole numbers, or complex patterns based on your specific needs.
The regular expression used in the example is:
import re r"[-+]?\d*\.\d+|\d+"Let's break down each parameter:
- '[-+]?': This allows for an optional sign (`+` or `-`) at the start of the number.
- '\d*\.\d+': matches zero or more digits before the decimal point.
- '\.': is used to match the decimal point itself.
- '\d+' : matches one or more digits after the decimal point.
- '|': This acts as an OR operator to include other possible matches.
- '\d+': This matches whole numbers, which do not have decimal points.
In this example, the string contains both positive and negative floating-point numbers, and also includes whole numbers. We going to extract them using Python regex.
import re s = "Sound Level: -11.7 db or 15.2 or 8 db" result = re.findall(r"[-+]?\d*\.\d+|\d+", s) print (result)
This gives the following output.
['-11.7', '15.2', '8']
Using Capturing Groups
Capturing groups are a powerful feature in regular expressions that allows you to extract specific portions of text from a larger string. They are created by placing parentheses around a portion of the regex pattern.
For more complex scenarios, such as extracting numbers alongside their units, you can use capturing groups.
Example
In this example, we are going to extract floating-point numbers using capturing groups.
import re s="Sound Level: -11.7 db or 15.2 or 8 db" result_with_units = re.findall(r"([-+]?\d*\.\d+|\d+)\s*(\w+)", s) print(result_with_units)
This is the output for the above code.
[('-11.7', 'db'), ('15.2', 'db'), ('8', 'db')]
Matching Only Floating Numbers
If you only want to extract floating-point numbers (excluding whole numbers), you can modify the regex
Example
The following example demonstrates extracting only floating-point numbers by modifying the regex.
import re s="Sound Level: -11.7 db or 15.2 or 8 db" result_floats = re.findall(r"[-+]?\d*\.\d+", s) print(result_floats)
Following is the output for the above example.
['-11.7', '15.2']