US Driver's License (Generic, State-Dependent) Regex for Java
/^[A-Z0-9]{1,2}[0-9]{1,18}$|^[A-Z][0-9]{3,8}[A-Z0-9]{0,3}$/iWhat this pattern does
This page provides a well-structured, multi-part regular expression for matching us driver's license (generic, state-dependent), ported and verified for Java. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// US Driver's License (Generic, State-Dependent)
// ReDoS-safe | RegexVault — Identity & PII > Driver's License Numbers
import java.util.regex.Pattern;
public class UsDriversLicenseGenericStatedependentValidator {
private static final Pattern PATTERN =
Pattern.compile("^[A-Z0-9]{1,2}[0-9]{1,18}$|^[A-Z][0-9]{3,8}[A-Z0-9]{0,3}$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("A12345678")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
A12345678 | ABCDEFGHIJKLMNOPQR |
123456789 | !234567 |
A000000000 | — |
D1234567 | — |
A1 | — |
When to use this pattern
This pattern is drawn from the Identity & PII > Driver's License Numbers category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Without knowing the issuing state, US DL format validation is extremely permissive. Always capture the issuing state alongside the DL number. State DMV formats change over time without notice.
Technical Notes
US driver's license formats vary dramatically by state. California: 1 letter + 7 digits. Texas: 8 digits. New York: 9 digits or 1 letter + 18 digits. Florida: 1 letter + 12 digits. Use a state-specific library (e.g., driver-license-validator) for precise validation.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern