Check if a String Contains only Alphabets in Java using Regex Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: FalseAlgorithmGet the stringMatch 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 class class 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)); } } Output: 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 valuesCheck if a string contains only alphabets in Java using Lambda expression Comment More infoAdvertise with us Next Article Check if a String Contains only Alphabets in Java C code_r Follow Improve Article Tags : Java Java-Strings java-regular-expression Java-String-Programs Practice Tags : JavaJava-Strings Similar Reads Check if a String Contains only Alphabets in Java In Java, to check if a string contains only alphabets, we have to verify each character to make sure it falls within the range of valid alphabetic characters. There are various ways to check this in Java, depending on requirements.Example: The most common and straightforward approach to validate if 4 min read Check if a String Contains Only Alphabets in Java Using Lambda Expression Lambda expressions basically express instances of functional interfaces (An interface with a single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces. Given a String 3 min read Extracting each word from a String using Regex in Java Given a string, extract words from it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Funny?? are not you? Output : Funny are not you Input : Geeks for geeks?? Output : Geeks for geeks We have discussed a solut 2 min read Check if a string consists only of special characters Given string str of length N, the task is to check if the given string contains only special characters or not. If the string contains only special characters, then print âYesâ. Otherwise, print âNoâ. Examples: Input: str = â@#$&%!~âOutput: YesExplanation: Given string contains only special char 9 min read Get the first letter of each word in a string using regex in Java Given a string, extract the first letter of each word in it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Geeks for geeks Output :Gfg Input : United Kingdom Output : UK Below is the Regular expression to extr 1 min read Print first letter of each word in a string using regex Given a string, extract the first letter of each word in it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Geeks for geeksOutput :Gfg Input : United KingdomOutput : UKBelow is the Regular expression to extract 3 min read Like