See how to use this form of Regex below to identify whether or not a string has only alphabets present.
public class RegexHasOnlyAlphabets{ public static void main(String args[]) { RegexHasOnlyAlphabets regexHasOnlyAlphabets = new RegexHasOnlyAlphabets(); regexHasOnlyAlphabets.proceed(); } public void proceed() { String inputStr = "HasOnlyAplhabets"; System.out.println("Has only alphabets: " + inputStr + ": " + checkIfInputHasOnlyAlphabets(inputStr)); inputStr = "HasOnly3Aplhabets"; System.out.println("Has only alphabets: " + inputStr + ": " + checkIfInputHasOnlyAlphabets(inputStr)); } public boolean checkIfInputHasOnlyAlphabets(String strArg) { return ( (null != strArg) && (! strArg.equals("")) && (strArg.matches("^[a-zA-Z]*$")) ); } }/*
Expected output:
[[email protected]]# java RegexHasOnlyAlphabetsHas only alphabets: HasOnlyAplhabets: trueHas only alphabets: HasOnly3Aplhabets: false*/