py4u blog

Finding All Numbers in a String Using Regular Expressions in Python

Regular expressions (regex) are a powerful tool for pattern matching in strings. In Python, the re module provides support for working with regular expressions. One common task is to extract all the numbers from a given string. This blog post will guide you through the process of using regular expressions in Python to achieve this.

2026-08

Table of Contents#

  1. Understanding Regular Expressions for Number Matching
  2. Using the re.findall() Function
  3. Example Usage
  4. Common Practices and Best Practices
  5. References

Understanding Regular Expressions for Number Matching#

To find numbers in a string, we can use a regular expression pattern. A simple pattern to match integers is \d+, where:

  • \d is a metacharacter that matches any digit (0 - 9).
  • + is a quantifier that means "one or more" of the preceding element (in this case, digits).

If we also want to match floating-point numbers, we can use a more complex pattern like [-+]?\d*\.\d+|\d+. Here's what each part means:

  • [-+]?: Optionally matches a plus or minus sign at the beginning.
  • \d*: Matches zero or more digits before the decimal point (for floating-point numbers).
  • \.: Matches the decimal point (needs to be escaped with a backslash because . has a special meaning in regex).
  • \d+: Matches one or more digits after the decimal point (for floating-point numbers).
  • |: Is a logical OR. So the whole pattern means either match the floating-point part or match \d+ (which is for integers).

Using the re.findall() Function#

In Python, the re.findall() function is used to find all non-overlapping matches of a pattern in a string. Its syntax is:

import re
re.findall(pattern, string)

Where pattern is the regular expression pattern we want to use, and string is the input string.

Example Usage#

Example 1: Matching Integers#

import re
 
text = "There are 12 apples and 3 oranges"
pattern = r"\d+"
numbers = re.findall(pattern, text)
print(numbers)

Output:

['12', '3']

In this example, we import the re module. Then we define our text string and the pattern \d+ (using a raw string r"\d+" to avoid issues with backslash escaping). We call re.findall() and store the result (a list of all the matched numbers as strings) in the numbers variable and print it.

Example 2: Matching Floating-Point Numbers#

import re
 
text = "The price is $12.50 and the discount is -3.2%"
pattern = r"[-+]?\d*\.\d+|\d+"
numbers = re.findall(pattern, text)
print(numbers)

Output:

['12.50', '-3.2']

Here, we use the more complex pattern for floating-point numbers. The re.findall() function correctly extracts both the positive floating-point number 12.50 and the negative one -3.2 from the text string.

Common Practices and Best Practices#

  • Use Raw Strings: When defining regex patterns in Python, it's a good practice to use raw strings (prefix the string with r). This helps avoid issues with backslash escaping. For example, r"\d+" instead of "\d+".
  • Test Your Patterns: Use online regex testers (like regex101.com) to test your patterns before implementing them in code. This can save you time debugging.
  • Handle Different Number Formats: If you expect numbers in different formats (e.g., with commas as thousand separators like 1,000), you may need to adjust your pattern. For example, to match 1,000 as a number, you could use a pattern like \d{1,3}(,\d{3})*(\.\d+)?|\d+ (but this can get quite complex depending on your requirements).

References#

By following the steps and examples in this blog post, you should be able to effectively use regular expressions in Python to find all the numbers in a given string.