- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Regex to extract maximum numeric value from a string
The maximum numeric value is extracted from an alphanumeric string. An example of this is given as follows −
String = abcd657efgh234 Maximum numeric value = 657
A program that demonstrates this is given as follows −
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main (String[] args) { String str = "123abc874def235ijk999"; System.out.println("The string is: " + str); String regex = "\d+"; Pattern ptrn = Pattern.compile(regex); Matcher match = ptrn.matcher(str); int maxNum = 0; while(match.find()) { int num = Integer.parseInt(match.group()); if(num > maxNum) { maxNum = num; } } System.out.println("The maximum numeric value in above string is: " + maxNum); } }
Output
The string is: 123abc874def235ijk999 The maximum numeric value in above string is: 999
Now let us understand the above program.
First, the string is displayed. Then, regular expression is created for at least one digit. Then regex is compiled and matcher object is created. The code snippet that demonstrates this is given as follows.
String str = "123abc874def235ijk999"; System.out.println("The string is: " + str); String regex = "\d+"; Pattern ptrn = Pattern.compile(regex); Matcher match = ptrn.matcher(str);
A while loop is used to find the maximum numeric value in the string. Then maxNum is displayed. The code snippet that demonstrates this is given as follows −
int maxNum = 0; while(match.find()) { int num = Integer.parseInt(match.group()); if(num > maxNum) { maxNum = num; } } System.out.println("The maximum numeric value in above string is: " + maxNum);
- Related Articles
- Python Regex to extract maximum numeric value from a string
- How to extract an HTML tag from a String using regex in Java?
- How to extract a group from a Java String that contains a Regex pattern
- Extract Numeric Date Value from Date Format in MySQL?
- How to extract multiple integers from a String in Java?
- How to extract certain substring from a string using Java?
- How to extract the maximum value from named vector in R?
- How to extract all string values from a vector in R with maximum lengths?
- Extracting each word from a string using Regex in Java
- Java regex to exclude a specific String constant
- How to extract the first n characters from a string using Java?
- How to extract the last n characters from a string using Java?
- How to match a character from given string including case using Java regex?
- How to separate string and a numeric value in R?
- Java Program to Check if a String is Numeric

Advertisements