How to check string contains ONLY numeric value in Java


In this post we will learn how to check string contains only numeric value using regex (regular expression) in java.

Regular expressions provides one of the simplest ways to find whether string contains only numeric value or not in java.

Example >
string '123' contains only numeric value
string '12345' contains only numeric value


string '123e' doesn't contains only numeric value
string '12a3' doesn't contains only numeric value
string '123@#' doesn't contains only numeric value


Example 1 to check string contains only numeric value in java using regex >
regex (Regular Expression) used = "\\d+"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsOnlyNumericExample1 {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("\\d+");
     String str = "1234";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains only numeric value");
     } else {
          System.out.println("string '"+str + "' doesn't contains only numeric value");
     }
}
}
/*
string '1234' contains only numeric value
*/



Example 2 to check string contains only numeric value in java using regex >
regex (Regular Expression) used = "[0-9]+"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsOnlyNumericExample2 {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("[0-9]+");
     String str = "1234";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains only numeric value");
     } else {
          System.out.println("string '"+str + "' doesn't contains only numeric value");
     }
}
}
/*
string '1234' contains only numeric value
*/



Example 3 to check string contains only numeric value in java using org.apache.commons.lang.StringUtils.isNumeric(str) >
import org.apache.commons.lang.StringUtils;
public class StringContainsOnlyNumericExample3_UsingCommons {
public static void main(String[] args) {
     String str = "1234";
     System.out.println(StringUtils.isNumeric(str));
}
}
/*
true
*/

Jar used in above program = commons-lang.jar



Example - to check string contains only numeric value, where passed string contains characters and numeric value (i.e string contains characters and digits) >
regex (Regular Expression) used = "[0-9]+"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsOnlyNumericExample {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("[0-9]+");
     String str = "12ab34";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains only numeric value");
     } else {
          System.out.println("string '"+str + "' doesn't contains only numeric value");
     }
}
}
/*
string '12ab34' doesn't contains only numeric value
*/


Labels: Core Java RegEx
eEdit
Must read for you :