Skip to content
Related Articles

Related Articles

Extract maximum numeric value from a given string | Set 2 (Regex approach)

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 13 Jul, 2022

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 : 365

In Set 1, we have discussed general approach for extract numeric value from given string.In this post, we will discuss regular expression approach for same. Below is one of the regular expression for at least one numeric digit.

\d+

So Regex solution is simple:

  1. Initialize MAX = 0
  2. 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.
  3. Return MAX at the last.

Implementation:

Java




// Java regex program to extract the maximum value
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG
{
    // Method to extract the maximum value
    static int extractMaximum(String str)
    {
        // regular expression for atleast one numeric digit
        String regex = "\\d+";
         
        // compiling regex
        Pattern p = Pattern.compile(regex);
         
        // Matcher object
        Matcher m = p.matcher(str);
         
        // initialize MAX = 0
        int MAX = 0;
         
        // loop over matcher
        while(m.find())
        {
            // convert numeric string to integer
            int num = Integer.parseInt(m.group());
             
            // compare num with MAX, update MAX if num > MAX
            if(num > MAX)
                MAX = num;
        }
         
        return MAX;
    }
 
    public static void main (String[] args)
    {
        String str = "100klh564abc365bg";
         
        System.out.println(extractMaximum(str));
    }
}


C#




// C# regex program to extract the maximum value
using System;
using System.Text.RegularExpressions;
 
class GFG
{
  // Method to extract the maximum value
  static int extractMaximum(string str)
  {
 
    // Regex object
    Regex regex = new Regex(@"\d+");
 
 
    // initialize MAX = 0
    int MAX = 0;
 
    // loop over matcher
    foreach (Match ItemMatch in regex.Matches(str))
    {
      // convert numeric string to integer
      int num = Int32.Parse(ItemMatch.Value);
 
      // compare num with MAX, update MAX if num > MAX
      if(num > MAX)
        MAX = num;
    }
 
    return MAX;   
  }
 
  static public void Main ()
  {
    string str = "100klh564abc365bg"
    Console.WriteLine(extractMaximum(str));
  }
}
 
// This code is contributed by kothavvsaakash


Output

564

Time complexity : O(n) 
Auxiliary Space : O(1)

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. 

Implementation:

Java




// Java regex program to extract the maximum value
// in case of large numbers
 
import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG
{
    // Method to extract the maximum value
    static BigInteger extractMaximum(String str)
    {
        // regular expression for atleast one numeric digit
        String regex = "\\d+";
         
        // compiling regex
        Pattern p = Pattern.compile(regex);
         
        // Matcher object
        Matcher m = p.matcher(str);
         
        // initialize MAX = 0
        BigInteger MAX = BigInteger.ZERO;
         
        // loop over matcher
        while(m.find())
        {
            // convert numeric string to BigInteger
            BigInteger num = new BigInteger(m.group());
             
            // compare num with MAX, update MAX if num > MAX
            if(num.compareTo(MAX) > 0)
                MAX = num;
        }
         
        return MAX;
    }
 
    public static void main (String[] args)
    {
        String str = "100klh564231315151313151315abc365bg";
         
        System.out.println(extractMaximum(str));
    }
}


Output

564231315151313151315

Time complexity : O(n) 
Auxiliary Space : O(1)

This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!