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:
[root@mypc]# java RegexHasOnlyAlphabets
Has only alphabets: HasOnlyAplhabets: true
Has only alphabets: HasOnly3Aplhabets: false
*/
Visit the DevX Tip Bank