Open In App

Python Regex to extract maximum numeric value from a string

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an alphanumeric string, extract maximum numeric value from that string. Alphabets will only be in lower case.

Examples:

Input : 100klh564abc365bg
Output : 564
Maximum numeric value among 100, 564
and 365 is 564.

Input : abchsd0sdhs
Output : 0

Python Regex to extract maximum numeric value from a string

This problem has existing solution please refer Extract maximum numeric value from a given string | Set 1 (General approach) link. We will solve this problem quickly in python using Regex. Approach is very simple,

  1. Find list of all integer numbers in string separated by lower case characters using re.findall(expression,string) method.
  2. Convert each number in form of string into decimal number and then find max of it.

Implementation:

Python




# Function to extract maximum numeric value from
# a given string
import re
 
def extractMax(input):
 
     # get a list of all numbers separated by
     # lower case characters
     # \d+ is a regular expression which means
     # one or more digit
     # output will be like ['100','564','365']
     numbers = re.findall('\d+',input)
 
     # now we need to convert each number into integer
     # int(string) converts string into integer
     # we will map int() function onto all elements
     # of numbers list
     numbers = map(int,numbers)
 
     print max(numbers)
 
# Driver program
if __name__ == "__main__":
    input = '100klh564abc365bg'
    extractMax(input)


Output

564

Time complexity : O(n), where n is the length of the input string. This is because the function iterates through the input string once to find all the numbers using the regular expression, and then iterates through the numbers again to convert them to integers.

Space complexity :  O(n)


Previous Article
Next Article

Similar Reads

Extract maximum numeric value from a given string | Set 2 (Regex approach)
Given an alphanumeric string, extract maximum numeric value from that string. Examples: Input : 100klh564abc365bg Output : 564 Maximum numeric value among 100, 564 and 365 is 564. Input : abchsd0365sdhs Output : 365Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. In Set 1, we have discussed general approach for ex
7 min read
Extract maximum numeric value from a given string | Set 1 (General approach)
Given an alphanumeric string, extract maximum numeric value from that string. Alphabets will only be in lower case. One approach to solving the problem is discussed here, other using Regular expressions is given in Set 2 Examples: Input : 100klh564abc365bg Output : 564 Maximum numeric value among 100, 564 and 365 is 564. Input : abchsd0sdhs Output
12 min read
Python program to Count Uppercase, Lowercase, special character and numeric values using Regex
Prerequisites: Regular Expression in Python Given a string. The task is to count the number of Uppercase, Lowercase, special character and numeric values present in the string using Regular expression in Python. Examples: Input : "ThisIsGeeksforGeeks!, 123" Output :No. of uppercase characters = 4No. of lowercase characters = 15No. of numerical char
2 min read
Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String
Given string str of length N, the task is to remove uppercase, lowercase, special, numeric, and non-numeric characters from this string and print the string after the simultaneous modifications. Examples: Input: str = “GFGgfg123$%” Output: After removing uppercase characters: gfg123$% After removing lowercase characters: GFG123$% After removing spe
10 min read
Python - Extract String till Numeric
Given a string, extract all its content till first appearance of numeric character. Input : test_str = "geeksforgeeks7 is best" Output : geeksforgeeks Explanation : All characters before 7 are extracted. Input : test_str = "2geeksforgeeks7 is best" Output : "" Explanation : No character extracted as 1st letter is numeric. Method #1: Using isdigit()
5 min read
Python program to extract numeric suffix from string
Given a string of characters with digits embedded in it. The task is to write a Python program to extract all the numbers that are trailing, i.e at the suffix of the string. Examples: Input : test_str = "GFG04"Output : 04Explanation : 04 is suffix of string and number hence extracted. Input : test_str = "GFG143"Output : 143Explanation : 143 is suff
7 min read
Python Extract Substring Using Regex
Python provides a powerful and flexible module called re for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple and commonly used methods to extract substrings usi
2 min read
Python - Extract ith Key's Value of K's Maximum value dictionary
Given Dictionary List, extract i'th keys value depending upon Kth key's maximum value. Input : test_list = [{"Gfg" : 3, "is" : 9, "best" : 10}, {"Gfg" : 8, "is" : 11, "best" : 19}, {"Gfg" : 9, "is" : 16, "best" : 1}], K = "best", i = "is" Output : 11 Explanation : best is max at 19, its corresponding "is" value is 11. Input : test_list = [{"Gfg" :
9 min read
Extract punctuation from the specified column of Dataframe using Regex
Prerequisite: Regular Expression in Python In this article, we will see how to extract punctuation used in the specified column of the Dataframe using Regex. Firstly, we are making regular expression that contains all the punctuation: [!"\$%&\'()*+,\-.\/:;=#@?\[\\\]^_`{|}~]* Then we are passing each row of specific column to re.findall() functi
2 min read
Extract date from a specified column of a given Pandas DataFrame using Regex
In this article, we will discuss how to extract only valid date from a specified column of a given Data Frame. The extracted date from the specified column should be in the form of 'mm-dd-yyyy'. Approach: In this article, we have used a regular expression to extract valid date from the specified column of the data frame. Here we used \b(1[0-2]|0[1-
2 min read