How to check string is alphanumeric or not using Regular Expression
Given string str, the task is to check whether the string is alphanumeric or not by using Regular Expression.
An alphanumeric string is a string that contains only alphabets from a-z, A-Z and some numbers from 0-9.
Examples:
Input: str = “GeeksforGeeks123”
Output: true
Explanation:
This string contains all the alphabets from a-z, A-Z, and the number from 0-9. Therefore, it is an alphanumeric string.
Input: str = “GeeksforGeeks”
Output: false
Explanation:
This string contains all the alphabets from a-z, A-Z, but doesn’t contain any number from 0-9. Therefore, it is not an alphanumeric string.
Input: str = “GeeksforGeeks123@#”
Output: false
Explanation:
This string contains all the alphabets from a-z, A-Z, and the number from 0-9 along with some special symbols. Therefore, it is not an alphanumeric string.
Approach: This problem can be solved by using Regular Expression.
- Get the string.
- Create a regular expression to check string is alphanumeric or not as mentioned below:
regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$";
- Where:
- ^ represents the starting of the string
- (?=.*[a-zA-Z]) represents the alphabets from a-z, A-Z
- (?=.*[0-9]) represents any number from 0-9
- [A-Za-z0-9] represents whether everything is either alphabetical or digit
- + represents one or more times
- $ represents the ending of the string
- Match the given string with the regex, in Java, this can be done by using Pattern.matcher()
- Return true if the string matches with the given regex, else return false
Below is the implementation of the above approach.
Java
// Java program to check string is// alphanumeric or not using Regular Expression.import java.util.regex.*;class GFG { // Function to check string is alphanumeric or not public static boolean isAlphaNumeric(String str) { // Regex to check string is alphanumeric or not. String regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$"; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the string is empty // return false if (str == null) { return false; } // Pattern class contains matcher() method // to find matching between given string // and regular expression. Matcher m = p.matcher(str); // Return if the string // matched the ReGex return m.matches(); } // Driver Code. public static void main(String args[]) { // Test Case 1: String str1 = "GeeksforGeeks123"; System.out.println( str1 + ": " + isAlphaNumeric(str1)); // Test Case 2: String str2 = "GeeksforGeeks"; System.out.println( str2 + ": " + isAlphaNumeric(str2)); // Test Case 3: String str3 = "GeeksforGeeks123@#"; System.out.println( str3 + ": " + isAlphaNumeric(str3)); // Test Case 4: String str4 = "123"; System.out.println( str4 + ": " + isAlphaNumeric(str4)); // Test Case 5: String str5 = "@#"; System.out.println( str5 + ": " + isAlphaNumeric(str5)); }} |
Python3
# Python3 program to check# string is alphanumeric or# not using Regular Expression.import re# Function to check string# is alphanumeric or notdef isAlphaNumeric(str): # Regex to check string is # alphanumeric or not. regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$" # Compile the ReGex p = re.compile(regex) # If the string is empty # return false if(str == None): return False # Return if the string # matched the ReGex if(re.search(p, str)): return True else: return False# Driver Code# Test Case 1:str1 = "GeeksforGeeks123"print(str1, ":", isAlphaNumeric(str1))# Test Case 2:str2 = "GeeksforGeeks"print(str2, ":", isAlphaNumeric(str2))# Test Case 3:str3 = "GeeksforGeeks123@#"print(str3, ":", isAlphaNumeric(str3))# Test Case 4:str4 = "123"print(str4, ":", isAlphaNumeric(str4))# Test Case 5:str5 = "@#"print(str5, ":", isAlphaNumeric(str5))# This code is contributed by avanitrachhadiya2155 |
GeeksforGeeks123: true GeeksforGeeks: false GeeksforGeeks123@#: false 123: false @#: false
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


