regex - Using Regular Expressions to Extract a Value in Java

ID : 20080

viewed : 8

Tags : javaregexjava

Top 5 Answer for regex - Using Regular Expressions to Extract a Value in Java

vote vote


Full example:

private static final Pattern p = Pattern.compile("^([a-zA-Z]+)([0-9]+)(.*)"); public static void main(String[] args) {     // create matcher for pattern p and given string     Matcher m = p.matcher("Testing123Testing");      // if an occurrence if a pattern was found in a given string...     if (m.find()) {         // ...then you can use group() methods.         System.out.println(; // whole matched expression         System.out.println(; // first expression from round brackets (Testing)         System.out.println(; // second one (123)         System.out.println(; // third one (Testing)     } } 

Since you're looking for the first number, you can use such regexp:


and will return you the first number. Note that signed numbers can contain a minus sign:

vote vote


import java.util.regex.Matcher; import java.util.regex.Pattern;  public class Regex1 {     public static void main(String[]args) {         Pattern p = Pattern.compile("\\d+");         Matcher m = p.matcher("hello1234goodboy789very2345");         while(m.find()) {             System.out.println(;         }     } } 


1234 789 2345 
vote vote


Allain basically has the java code, so you can use that. However, his expression only matches if your numbers are only preceded by a stream of word characters.


should be able to find the first string of digits. You don't need to specify what's before it, if you're sure that it's going to be the first string of digits. Likewise, there is no use to specify what's after it, unless you want that. If you just want the number, and are sure that it will be the first string of one or more digits then that's all you need.

If you expect it to be offset by spaces, it will make it even more distinct to specify


might be better.

If you need all three parts, this will do:


EDIT The Expressions given by Allain and Jack suggest that you need to specify some subset of non-digits in order to capture digits. If you tell the regex engine you're looking for \d then it's going to ignore everything before the digits. If J or A's expression fits your pattern, then the whole match equals the input string. And there's no reason to specify it. It probably slows a clean match down, if it isn't totally ignored.

vote vote


In addition to Pattern, the Java String class also has several methods that can work with regular expressions, in your case the code will be:

"ab123abc".replaceFirst("\\D*(\\d*).*", "$1") 

where \\D is a non-digit character.

vote vote


In Java 1.4 and up:

String input = "..."; Matcher matcher = Pattern.compile("[^0-9]+([0-9]+)[^0-9]+").matcher(input); if (matcher.find()) {     String someNumberStr =;     // if you need this to be an int:     int someNumberInt = Integer.parseInt(someNumberStr); } 

Top 3 video Explaining regex - Using Regular Expressions to Extract a Value in Java