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.
Recommended Posts:
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if an URL is valid or not using Regular Expression
- How to check Aadhar number is valid or not using Regular Expression
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Remove duplicate words from Sentence using Regular Expression
- How to validate identifier using Regular Expression in Java
- How to validate time in 12-hour format using Regular Expression
- Validating Roman Numerals Using Regular expression
- How to validate time in 24-hour format using Regular Expression
- How to validate pin code of India using Regular Expression
- How to validate Visa Card number using Regular Expression
- How to validate Indian Passport number using Regular Expression
- How to validate PAN Card number using Regular Expression
- How to validate Hexadecimal Color Code using Regular Expression
- Finding Data Type of User Input using Regular Expression in Java
- How to validate GST (Goods and Services Tax) number using Regular Expression
- How to validate HTML tag using Regular Expression
- How to validate a domain name using Regular Expression
- How to validate image file extension using Regular Expression
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.
Improved By : avanitrachhadiya2155

