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
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,
- Find list of all integer numbers in string separated by lower case characters using re.findall(expression,string) method.
- Convert each number in form of string into decimal number and then find max of it.
# 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Python - Extract String till Numeric
- Python program to Count Uppercase, Lowercase, special character and numeric values using Regex
- Python - Extract ith Key's Value of K's Maximum value dictionary
- Extract punctuation from the specified column of Dataframe using Regex
- Extract date from a specified column of a given Pandas DataFrame using Regex
- Python - Extract Tuples with all Numeric Strings
- Python - Extract Item with Maximum Tuple Value
- Python - Extract Maximum Keys' value dictionaries
- Python - Maximum Pair Summation in numeric String
- Python | Extract key-value of dictionary in variables
- Python - Extract Unique value key pairs
- Python - Extract Kth index elements from Dictionary Value list
- Python - Extract Key's value from Mixed Dictionaries List
- Python - Extract ith element of K key's value
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- The most occurring number in a string using Regex in python
- Python Regex - Program to accept string starting with vowel
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

