Open In App

Python Regex to extract maximum numeric value from a string

Last Updated : 07 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

When working with strings containing both text and numbers, it’s common to extract the largest numeric value embedded within the text, Python’s re module provides an efficient and concise way to achieve this. In this article, we will explore various methods for this

Using re.findall and max

This is the most simple and efficient approach, it uses the re.findall() method to extract all numeric values and the max() function to find the largest among them.

import re

s = "The price is 120 dollars, and the discount is 50, saving 70 more."

# Extracting all numeric values
n = re.findall(r'\d+', s)

# Finding the maximum value
m = max(map(int, n))
print(m)

Output
120

Explanation:

  • re.findall() extracts all sequences of digits (numeric values) as strings.
  • map(int, n) converts the extracted strings into integers.
  • max finds the maximum numeric value.

Let’s explore some more methods and see how Regex is used to extract maximum numeric value from a string.

Using re.finditer for Edge Cases

re.finditer() method avoids creating a full list of matches making it memory-efficient for large strings.

import re

s = "The price is 120 dollars, and the discount is 50, saving 70 more."

# Finding maximum numeric value directly
n = max(int(match.group()) for match in re.finditer(r'\d+', s))
print(n)

Output
120

Explanation:

  • re.finditer iterates over each match instead of creating a list.
  • match.group() retrieves the matched numeric value as a string.
  • max directly computes the maximum from the generator.

Custom Parsing with Regex and Loop

If we want explicit control over the process, a custom loop can achieve the same result.

import re

s = "The price is 120 dollars, and the discount is 50, saving 70 more."

# Initializing the maximum value
m = float('-inf')

# Using regex to find numeric values
for match in re.finditer(r'\d+', s):
    num = int(match.group())
    m = max(m, num)

print(m)

Output
120

Explanation:

  • re.finditer finds numeric values one by one.
  • Each numeric value is compared with the current maximum.
  • While slightly less concise, this method provides full control.

Using List Comprehension with Regex

For those who prefer a more compact approach, we can combine list comprehension and re.findall().

import re

s = "The price is 120 dollars, and the discount is 50, saving 70 more."
#max_value
m = max([int(num) for num in re.findall(r'\d+', s)])
print(m)

Output
120

Explanation:

  • This method uses a one-liner list comprehension to extract and convert numeric values, followed by finding the maximum value.

Next Article

Similar Reads

three90RightbarBannerImg