How to Check String is Alphanumeric or Not Using Regular Expression

If we have a string and want to know whether it is either alphanumeric or not, we can do it using Regular Expression(regex).

What is Regular Expression?

A Regular Expressionor regexis a series of characters and symbols representing a pattern or string to be explored in a lengthy text.

History of Regular Expression

The word “Regular Expression” came into existence in 1951, when regular languages were described by mathematician Stephen Cole Kleene using his mathematical script called regular events.

Use of Regular Expressions

These expressions became popular in 1968, especially for lexical analysis in a compiler and pattern matching in a text editor.

Regular Expressions are widely used in text fining tasks, and generally in the processing of strings, where data present may or may not be textual. Some of its common applications are Data Collection, Simple Parsing, etc.

Advantages of Regular Expressions

  • It can be used widely. For example, we can create one regular expression according to our need.
  • Almost every programming language supports these expressions, only a few of them does not understand regular expressions.
  • Greater number of tasks can be performed using them.
  • Code is more optimized and looks cleaner.
  • Faster authentication. For example, instead of using multiple If and Else conditions, we can authenticate in one go using them.

Note: The string containing both alphabets (from a-z, A-Z) and numbers (from 0-9) only is called an alphanumeric string. A string containing any other characters is not considered alphanumeric.)

Examples:

Input: st = “GoodMorning123” 
Output: True 

Description:

Since string ‘st’ contains alphabets (a-z, A-Z) and numbers (0-9) only, therefore it’s an alphanumeric string.

Input: st = “GoodMorning” 
Output: False 

Description:

Since string ‘st’ contains alphabets (a-z, A-Z) but does not contain numbers (0-9), therefore it is not an alphanumeric string.

Input: st = “Good_Morning!@#” 
Output: False 

Description:

Since string ‘st’ contains alphabets (a-z, A-Z) but it does not contain numbers (0-9) and in addition to that it also contains some special characters, therefore it is not an alphanumeric string.

Input: st = “GoodMorning123!@#”
Output: False

Description:

Since string ‘st’ contains alphabets (a-z, A-Z) and numbers (0-9) but in addition to that it also has some special characters, therefore it cannot be considered an alphanumeric string.

Now, what is the procedure to be followed to get the required work done?

Firstly, we will create a function to check whether the string is alphanumeric or not using Regular Expression as shown below:

regex = “^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$”;

In the above expression:

  • ^is regarded as the beginning of the string.
  • “^(?=.*[a-zA-Z])shows the alphabets from ‘a to z’ in both uppercase and lowercase.
  • (?=.*[0-9]) shows any number from ‘0 to 9’.
  • [A-Za-z0-9] represents whether every character is either an alphabet or a digit.
  • + represents a number of repetitions.
  • $ is regarded as the end of the string.

Then we will use the given string with regex and if the given string matches with the regex, we will return True as output or else return False as output.

Example:

// A program in java to check whether the given string
// is alphanumeric or not using regex
 
importjava.util.regex.*;
 
classGFG {
 
    //User-defined function to check the given string
    publicstaticbooleanisAN(String st)
    {
        // initialization of regex
        String regex = “^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$”;
 
        // Compiling regex
        Pattern x = Pattern.compile(regex);
 
        //return false if there is no content in the string
        if(st == null) {
            returnfalse;
        }
 
        // matcher() is the pre-defined used to compare
        // given string and regular function
        Matcher y = x.matcher(str);
 
        // Give back if the given string
        // matcheswithregex
        returny.matches();
    }
 
    // main code
    publicstaticvoidmain(String args[])
    {
 
        // case-I:
        String st1 = “GoodMorning1234”;
        System.out.println(
            St1 + “: “
            + isAN(st1));
 
        // case-II:
        String st2 = “GoodMorning”;
        System.out.println(
            st2 + “: “
            + isAN(st2));
 
        // case-III:
        String st3 = “Good_Morning123!@#”;
        System.out.println(
            st3 + “: “
            + isAN(st3));
 
        // case-IV:
        String st4 = “12345”;
        System.out.println(
            st4 + “: “
            + isAN(st4));
 
        // case- V:
        String st5 = “!@#$”;
        System.out.println(
            st5 + “: “
            + isAN(st5));
    }
}

Output:

GoodMorning1234: True
GoodMorning: False
Good_Morning123!@#: False
12345: False
!@#$: False