Given an alphanumeric string, extract maximum numeric value from that string.
Input : 100klh564abc365bg Output : 564 Maximum numeric value among 100, 564 and 365 is 564. Input : abchsd0365sdhs Output : 365
Below is one of the regular expression for at least one numeric digit.
So Regex solution is simple:
- Initialize MAX = 0
- Run loop over matcher, whenever match found, convert numeric string to integer and compare it with MAX.
- If number greater than MAX, update MAX to number.
- Return MAX at the last.
But above program wouldn’t work if number is greater that integer range. You can try parseLong() method for numbers upto long range.But to handle the case of large numbers(greater than long range) we can take help of BigInteger class in java. Below is the java program to demonstrate the same.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Python Regex to extract maximum numeric value from a string
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Check if a string contains only alphabets in Java using Regex
- Print first letter of each word in a string using regex
- Extracting each word from a String using Regex in Java
- Get the first letter of each word in a string using regex in Java
- Program to find all match of a regex in a string
- Extract all integers from string in C++
- Program to extract words from a given String
- Extract all integers from the given string in Java
- Splitting a Numeric String
- Extract and print words separately from a given Camel Case string
- Split numeric, alphabetic and special symbols from a String
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Find all the patterns of "1(0+)1" in a given string | SET 1(General Approach)
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)