A String is said to be alphanumeric if,
- It contains lowercase alphabets from 'a' to 'z'.
- It contains uppercase alphabets from 'A' to 'Z'.
- It contains digits from '0' to '9'.
- It does not contain any special characters, punctuation marks, or spaces.
- The string may contain a combination of lowercase letters, uppercase letters, and digits in any order.
- The length of the string can be any positive integer, including zero (empty string).
Examples:
| String | Validity |
|---|---|
| "abc123" | Valid |
| "HelloWorld" | Valid |
| "123456" | Valid |
| "AbCdEfG" | Valid |
| "aBc123" | Valid |
| "a1b2c3" | Valid |
| "Alphanumeric123" | Valid |
| "Invalid String!" | Invalid |
| "Spaces Are NotAllowed" | Invalid |
| "Special@Characters" | Invalid |
| "123_456" | Invalid |
| "" | Valid |
Now that we know what Alphanumerics are, let's take an example using Java RegEx.
package org.code2care.examples;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class AlphanumericRegExValidation {
public static void main(String[] args) {
List<String> strings = Arrays.asList(
"abc123",
"HelloWorld",
"123456",
"AbCdEfG",
"aBc123",
"a1b2c3",
"Alphanumeric123",
"Invalid String!",
"Spaces Are NotAllowed",
"Special@Characters",
"123_456",
""
);
Pattern pattern = Pattern.compile("^[a-zA-Z0-9]+$");
for (String str : strings) {
boolean isValid = pattern.matcher(str).matches();
System.out.println("String: " + str + " is Alphanumeric: " + isValid);
}
}
}

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!