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:
- 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.
Implementation:
C++
#include <iostream>
#include <regex>
using namespace std;
int extractMaximum(string str)
{
string regex = "\\d+" ;
std::regex r(regex);
std::sregex_iterator it(str.begin(), str.end(), r);
std::sregex_iterator end;
int MAX = 0;
while (it != end) {
int num = stoi(it->str());
if (num > MAX)
MAX = num;
it++;
}
return MAX;
}
int main()
{
string str = "100klh564abc365bg" ;
cout << extractMaximum(str);
return 0;
}
|
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG
{
static int extractMaximum(String str)
{
String regex = "\\d+" ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
int MAX = 0 ;
while (m.find())
{
int num = Integer.parseInt(m.group());
if (num > MAX)
MAX = num;
}
return MAX;
}
public static void main (String[] args)
{
String str = "100klh564abc365bg" ;
System.out.println(extractMaximum(str));
}
}
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static int extractMaximum( string str)
{
Regex regex = new Regex( @"\d+" );
int MAX = 0;
foreach (Match ItemMatch in regex.Matches(str))
{
int num = Int32.Parse(ItemMatch.Value);
if (num > MAX)
MAX = num;
}
return MAX;
}
static public void Main ()
{
string str = "100klh564abc365bg" ;
Console.WriteLine(extractMaximum(str));
}
}
|
Python3
import re
def extractMaximum( str ):
regex = "\\d+"
p = re. compile (regex)
MAX = 0
for m in re.finditer(p, str ):
num = int (m.group( 0 ))
if (num > MAX ):
MAX = num
return MAX
str = "100klh564abc365bg"
print (extractMaximum( str ))
|
Javascript
function extractMaximum(str) {
let regex = /\d+/g;
let MAX = 0;
let match;
while ((match = regex.exec(str)) !== null ) {
let num = parseInt(match[0]);
if (num > MAX) {
MAX = num;
}
}
return MAX;
}
let str = "100klh564abc365bg" ;
console.log(extractMaximum(str));
|
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:
C++
#include <iostream>
#include <regex>
#include <string>
#include <climits>
#include <sstream>
using namespace std;
long long extractMaximum(string str)
{
regex re( "\\d+" );
long long MAX = LLONG_MIN;
for (sregex_iterator i = sregex_iterator(str.begin(), str.end(), re); i != sregex_iterator(); i++) {
stringstream ss(i->str());
long long num = 0;
ss >> num;
if (num > MAX)
MAX = num;
}
return MAX;
}
int main()
{
string str = "100klh564231315151313151315abc365bg" ;
cout << extractMaximum(str) << endl;
return 0;
}
|
Java
import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG
{
static BigInteger extractMaximum(String str)
{
String regex = "\\d+" ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
BigInteger MAX = BigInteger.ZERO;
while (m.find())
{
BigInteger num = new BigInteger(m.group());
if (num.compareTo(MAX) > 0 )
MAX = num;
}
return MAX;
}
public static void main (String[] args)
{
String str = "100klh564231315151313151315abc365bg" ;
System.out.println(extractMaximum(str));
}
}
|
Python3
import re
def extractMaximum( str ):
regex = r '\d+'
MAX = 0
for m in re.finditer(regex, str ):
num = int (m.group())
if num > MAX :
MAX = num
return MAX
str = "100klh564231315151313151315abc365bg"
print (extractMaximum( str ))
|
C#
using System;
using System.Text.RegularExpressions;
using System.Numerics;
class GFG
{
static BigInteger ExtractMaximum( string str)
{
Regex re = new Regex( @"\d+" );
BigInteger MAX = BigInteger.MinValue;
foreach (Match m in re.Matches(str))
{
BigInteger num = BigInteger.Parse(m.Value);
if (num > MAX)
MAX = num;
}
return MAX;
}
static void Main( string [] args)
{
string str = "100klh564231315151313151315abc365bg" ;
Console.WriteLine(ExtractMaximum(str));
}
}
|
Javascript
function extractMaximum(str) {
const regex = /\d+/g;
let m;
let MAX = BigInt(0);
while ((m = regex.exec(str)) !== null ) {
let num = BigInt(m[0]);
if (num > MAX) {
MAX = num;
}
}
return MAX;
}
const str = "100klh564231315151313151315abc365bg" ;
console.log(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.
Please Login to comment...