South African ID Number Regex for Python
/^(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(\d{4})([01])(\d)(\d)$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching south african id number, 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
# South African ID Number
# ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
import re
south_african_id_number_pattern = re.compile(r'^(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(\d{4})([01])(\d)(\d)$')
def validate_south_african_id_number(value: str) -> bool:
return bool(south_african_id_number_pattern.fullmatch(value))
# Example
print(validate_south_african_id_number("8001015009087")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
8001015009087 | 080101500908 |
9202204720082 | 80010150090870 |
7601100800086 | 800101500908A |
| — | 800001500908 |
When to use this pattern
This pattern is drawn from the Identity & PII > National Identity 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
The ID number encodes race historically (a remnant of apartheid-era classification) — the digit is still present but is now always 8 for new IDs. Never use this digit for any purpose. Always validate the Luhn checksum.
Technical Notes
Structure: YYMMDD (DOB) + SSSS (gender sequence: 0000-4999=female, 5000-9999=male) + C (citizenship: 0=SA citizen, 1=permanent resident) + A (race, now always 8) + checksum (Luhn). Checksum must be Luhn-validated.
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