private static void onlyAlphabets () {
//Only Alphabets
String expression = "^[a-zA-Z]+$";
String str1 = "It is a string"; //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 + " - contains only alphabets");
found = true;
}
if(!found){
System.out.println(str1 + " - contains other than alphabets");
}
}
Sunday, July 10, 2011
Regular Expression - Validate only alphabets
Here is the simple program to validate the digits
Labels:
Java