US Driver's License (Generic, State-Dependent) Regex for Python
/^[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 Python. 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 Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# US Driver's License (Generic, State-Dependent)
# ReDoS-safe | RegexVault — Identity & PII > Driver's License Numbers
import re
us_drivers_license_generic_statedependent_pattern = re.compile(r'^[A-Z0-9]{1,2}[0-9]{1,18}$|^[A-Z][0-9]{3,8}[A-Z0-9]{0,3}$')
def validate_us_drivers_license_generic_statedependent(value: str) -> bool:
return bool(us_drivers_license_generic_statedependent_pattern.fullmatch(value))
# Example
print(validate_us_drivers_license_generic_statedependent("A12345678")) # TrueTest 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 Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. 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