ICAO MRZ Line 2 (Passport Data Line) Regex for Java
/^([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 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
// ICAO MRZ Line 2 (Passport Data Line)
// ReDoS-safe | RegexVault — Identity & PII > Passport Numbers
import java.util.regex.Pattern;
public class IcaoMrzLine2PassportDataLineValidator {
private static final Pattern PATTERN =
Pattern.compile("^([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<])$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("L898902C36UTO7408122F1204159ZE184226B<<<<<11")); // true
}
}Test 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 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
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