String regex = "[0-9]+"; // or String regex = "d+";
String str = "123";
System.out.println(str.matches(regex)); // true
str = "12a";
System.out.println(str.matches(regex)); // false
String regex = "^d{10}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("9876543210");
matcher.matches(); // returns true if pattern matches, else returns false