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



This article will discuss how to write a Python regular expression that matches floating point numbers. We will try to create a regular expression pattern that can match any floating point number.

So the regex we are going to create should also match integers and floating point numbers in which the integer part is not given. A floating-point number can be look like -

  • Whole numbers with decimals like 3.14, 1.5, 12.10,

  • Negative numbers like -2.55, -1.003,

  • Numbers with optional zeros like 007.5, .75.

Regex Pattern for Floating Point Number

Here is the regular expression pattern for identifying floating point number -

pat = r"[+-]?[0-9]+\.[0-9]+"

The above regex pattern will help you to find floating point numbers in a text. Where,

  • [+-]? this pattern means the number can have a plus (+) or minus (-) sign at the start and it can be optional.
  • And [0-9]+ means at least one digit appears before the decimal (.).

For example 3, 45, or 234. The \. shows the decimal point. The last pattern [0-9]+ means at least one digit should be there after the decimal. For example, .5, .75, or .0014.

Ways to Match Floating Point Numbers

There are several ways to match floating point numbers in Python, such as -

Example: Validate Floating Numbers in a Text

The following example will use the pattern we have created above and using this pattern we are going to match a floating point numbers in the given text. For this purpose we will use re.findall() function to find all floating point numbers in a string.

# Import re module
import re

# Define a string with various numbers
txt = "Pi is 3.14 and the temperature dropped to -2.5."

# Define a pattern to match floating-point numbers
pat = r"[+-]?[0-9]+\.[0-9]+"

# Find all matches of the pattern in the string
res = re.findall(pat, txt)

# Print the result
print(res) 

Output

This will create the below outcome -

['3.14', '-2.5']

Example: Validate User Input

This is another easy way to match floating point number to validate user input. We will use Python's re.fullmatch() method to match the given string with the regex pattern for validating floating point number.

Our result will be in boolean form if the string is a floating point number it will return True otherwise False.

# Import re module
import re

# Find all floating point numbers in a string
def is_float(num):
    # Check if the string matches the pattern for a floating point number  
    return bool(re.fullmatch(r"[+-]?[0-9]+\.[0-9]+", num))

# Print the results of the function with different inputs
print(is_float("12.5"))  
print(is_float("abc")) 

Output

This will generate the below result -

True
False

Example: Extract Only Decimal Numbers

In this program we will extract floating point numbers with decimals only from a given string using Python's re.findall() method. The method is used to search for all occurrences of a pattern within a text.

# Import re module
import re

# Find all floating point numbers in a string
txt = "Values are 3, 4.5, -0.75, and 100."

# Regular expression pattern to match floating point numbers
pat = r"[+-]?[0-9]+\.[0-9]+"  

# Find all matches of the pattern in the text
res = re.findall(pat, txt)

# Print the result
print(res)  

Output

This will produce the following result -

['4.5', '-0.75']

Example: Floating Number with Units

Now we have a string input in which we have some units with floating point numbers. So our task is to extract those numbers from the given string. Here we are using the same regex pattern we have used till now but with some modification in it.

We are adding (?=[a-z%]) this pattern in the end of the pattern so that it can look for the unit also in the given string.

# Import re module
import re

# Find all floating point numbers in a string
txt = "Weights: 2.5kg, -1.75m, 50, 10."

# Regular expression pattern to match floating point numbers
pat = r"[+-]?[0-9]+\.[0-9]+(?=[a-z%])"  

# find the pattern in the text
res = re.findall(pat, txt)

# Print the result
print(res)

Output

This will lead to the following outcome -

['2.5', '-1.75']
Updated on: 2025-06-06T15:26:39+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements