ICAO MRZ Line 2 (Passport Data Line) Regex for Python
/^([A-Z0-9<]{9})([0-9<])([A-Z]{3})(\d{6})([0-9<])([MFX<])(\d{6})([0-9<])([A-Z0-9<]{14})([0-9<])([0-9<])$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching icao mrz line 2 (passport data line), 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
# ICAO MRZ Line 2 (Passport Data Line)
# ReDoS-safe | RegexVault — Identity & PII > Passport Numbers
import re
icao_mrz_line_2_passport_data_line_pattern = re.compile(r'^([A-Z0-9<]{9})([0-9<])([A-Z]{3})(\d{6})([0-9<])([MFX<])(\d{6})([0-9<])([A-Z0-9<]{14})([0-9<])([0-9<])$')
def validate_icao_mrz_line_2_passport_data_line(value: str) -> bool:
return bool(icao_mrz_line_2_passport_data_line_pattern.fullmatch(value))
# Example
print(validate_icao_mrz_line_2_passport_data_line("L898902C36UTO7408122F1204159ZE184226B<<<<<11")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
L898902C36UTO7408122F1204159ZE184226B<<<<<11 | L898902C36UTO740812F1204159ZE184226B<<<<<1 |
AB1234567<USA8001010M2501016<<<<<<<<<<<<<<22 | L898902C36UTO7408122F120415ZE184226B<<<<<1 |
When to use this pattern
This pattern is drawn from the Identity & PII > Passport 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
Gender code X is used for non-binary/unspecified in ICAO Doc 9303 Amendment 8. Check digit validation requires the ICAO algorithm — format match alone is insufficient for fraud prevention.
Technical Notes
MRZ Line 2: 9-char passport number + check digit + 3-char nationality + 6-char DOB (YYMMDD) + check digit + gender (M/F/X/< ) + 6-char expiry + check digit + 14-char optional data + check digit + overall check digit. Capture groups: 1=passport number, 3=nationality, 4=DOB, 6=gender, 7=expiry.
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