private static void onlyDigits () { //Only Digits String expression = "^[0-9]+$"; String str1 = "123"; //It will match. String str2 = "2dF1"; //It wont match. String str3 = "a33345345"; //It wont match. Pattern pattern = Pattern.compile(expression); Matcher matcher = pattern.matcher(str1); boolean found = false; if (matcher.find()) { System.out.println (str1 + " - is a number"); found = true; } if(!found){ System.out.println(str1 + " - is not a number"); } }
Sunday, July 10, 2011
Regular Expression - Validate only digits
Here is the simple program to validate the digits
Labels:
Java