java check if string contains only digits using regex

Java
[Edit]
+
0
-
0

Java check if string contains only digits using regex

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringExample { private static final Pattern DIGITS_PATTERN = Pattern.compile("\\d+"); // or "[0-9]+" public static boolean containsOnlyDigits(String text) { Matcher matcher = DIGITS_PATTERN.matcher(text); return matcher.matches(); } public static void main(String[] args) { System.out.println(containsOnlyDigits("123456")); // true System.out.println(containsOnlyDigits("1234Sample text..." )); // false } }