How to check string is alphanumeric or not using Regular Expression

Given a 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.

  1. Get the string.
  2. 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
  3. Match the given string with the regex, in Java, this can be done by using Pattern.matcher()
  4. Return true if the string matches with the given regex, else return false

Below is the implementation of the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.