Check if a String Contains only Alphabets in Java using Regex

05 Apr, 2022
Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating, and editing a text. It is widely used to define a constraint on strings such as a password. Regular Expressions are provided under java.util.regex package.
For any string, here the task is to check whether a string contains only alphabets or not using Regex. Now for a given string, the characters of the string are checked one by one using Regex. Regex can be used to check a string for alphabets. String.matches() method is used to check whether or not the string matches the given regex.
^[a-zA-Z]*$
Illustrations:  
Input: GeeksforGeeks
Output: True

Input: Geeks4Geeks
Output: False

Input: null
Output: False
Get the stringMatch the string with the Regex using matches().Return true is matched
Get the string
Match the string with the Regex using matches().
Return true is matched
Pseudocode for the above algorithm is proposed below as follows:
public static boolean isStringOnlyAlphabet(String str) 
{
    return ((!str.equals(""))
            && (str != null)
            && (str.matches("^[a-zA-Z]*$")));
}
Example:
Java
// Java Program to Check If String Contains Only Alphabets// Using Regular Expression // Main classclass GFG {     // Method 1    // To check String for only Alphabets    public static boolean isStringOnlyAlphabet(String str)    {         return ((str != null) && (!str.equals(""))                && (str.matches("^[a-zA-Z]*$")));    }     // Method 2    // Main driver method    public static void main(String[] args)    {         // Calling out methods over string        // covering all scenarios         // Use case 1        System.out.println("Test Case 1:");        // Input string        String str1 = "GeeksforGeeks";        System.out.println("Input: " + str1);         System.out.println("Output: "                           + isStringOnlyAlphabet(str1));         // Use case 2        // Checking for String with numeric characters        System.out.println("\nTest Case 2:");        // Input string        String str2 = "Geeks4Geeks";        System.out.println("Input: " + str2);        System.out.println("Output: "                           + isStringOnlyAlphabet(str2));         // Use Case 3        // Checking for null String        System.out.println("\nTest Case 3:");        // Input string        String str3 = null;        System.out.println("Input: " + str3);        System.out.println("Output: "                           + isStringOnlyAlphabet(str3));         // Use Case 4        // Checking for empty String        System.out.println("\nTest Case 4:");        // Input string        String str4 = "";        System.out.println("Input: " + str4);        System.out.println("Output: "                           + isStringOnlyAlphabet(str4));    }}
Test Case 1:
Input: GeeksforGeeks
Output: true

Test Case 2:
Input: Geeks4Geeks
Output: false

Test Case 3:
Input: null
Output: false

Test Case 4:
Input: 
Output: false
 
Related Articles:
Check if a string contains only alphabets in Java using ASCII values
Check if a string contains only alphabets in Java using Lambda expression
solankimayank
simranarora5sos
java-regular-expression
Java-String-Programs
Java-Strings
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Collections in Java
Singleton Class in Java
Set in Java
Initializing a List in Java
Introduction to Java
Multithreading in Java
Constructors in Java
Exceptions in Java
LinkedList in Java