How to check string is alphanumeric or not using Regular Expression
Last Updated :
03 Jan, 2023
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.
C++
#include <iostream>
#include <regex>
using namespace std;
bool isAlphaNumeric(string str) {
const regex pattern( "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;;
}
else
{
return false ;
}
}
int main()
{
string str1= "GeeksforGeeks123" ;
cout <<isAlphaNumeric(str1) << endl;
string str2= "GeeksforGeeks" ;
cout << isAlphaNumeric(str2) << endl;
string str3= "GeeksforGeeks123@#" ;
cout << isAlphaNumeric(str3) << endl;
string str4= "123" ;
cout << isAlphaNumeric(str4) << endl;
string str5= "@#" ;
cout << isAlphaNumeric(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isAlphaNumeric(String str)
{
String regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "GeeksforGeeks123" ;
System.out.println(
str1 + ": "
+ isAlphaNumeric(str1));
String str2 = "GeeksforGeeks" ;
System.out.println(
str2 + ": "
+ isAlphaNumeric(str2));
String str3 = "GeeksforGeeks123@#" ;
System.out.println(
str3 + ": "
+ isAlphaNumeric(str3));
String str4 = "123" ;
System.out.println(
str4 + ": "
+ isAlphaNumeric(str4));
String str5 = "@#" ;
System.out.println(
str5 + ": "
+ isAlphaNumeric(str5));
}
}
|
Python3
import re
def isAlphaNumeric( str ):
regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "GeeksforGeeks123"
print (str1, ":" ,
isAlphaNumeric(str1))
str2 = "GeeksforGeeks"
print (str2, ":" ,
isAlphaNumeric(str2))
str3 = "GeeksforGeeks123@#"
print (str3, ":" ,
isAlphaNumeric(str3))
str4 = "123"
print (str4, ":" ,
isAlphaNumeric(str4))
str5 = "@#"
print (str5, ":" ,
isAlphaNumeric(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "GeeksforGeeks123" , "GeeksforGeeks" ,
"GeeksforGeeks123@#" , "123" ,
"@#" };
foreach ( string s in str) {
Console.WriteLine( isAlphaNumeric(s) ? "true" : "false" );
}
Console.ReadKey(); }
public static bool isAlphaNumeric( string str)
{
string strRegex = @"^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
function isAlphaNumeric(str) {
let regex = new RegExp(/^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "GeeksforGeeks123" ;
console.log(isAlphaNumeric(str1));
let str2 = "GeeksforGeeks" ;
console.log(isAlphaNumeric(str2));
let str3 = "GeeksforGeeks123@#" ;
console.log(isAlphaNumeric(str3));
let str4 = "#123" ;
console.log(isAlphaNumeric(str4));
let str5 = "#@#" ;
console.log(isAlphaNumeric(str5));
|
Output:
GeeksforGeeks123: true
GeeksforGeeks: false
GeeksforGeeks123@#: false
123: false
@#: false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...