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

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- git fatal: Authentication failed error [fix] - Git
- Add Custom External Link to SharePoint Site Navigation - SharePoint
- Java: max() and min() methods java.lang.Math - Java
- zsh: command not found: cls macOS Big Sur - MacOS
- How to fix command not found brew (bash, zsh) on macOS Terminal - MacOS
- Connect to local macOS/Windows localhost (127.0.0.1) from within Docker Container Image - Docker
- You can now Transfer Facebook Posts to Google Photos, Docs, Checkout how! - News
- How to Run PowerShell Script as a Windows Scheduler Task - Powershell