Identity
ICAO MRZ Passport Line 1 Regex
Passports follow the ICAO Doc 9303 TD3 layout, whose machine readable zone has two lines of 44 characters. The first line starts with P, an optional type character, a three-letter issuing state code and then the holder's name, with the filler character < separating surname, given names and padding. This pattern checks that shape, which helps OCR pipelines confirm they captured the right line before parsing. Germany's code D<< is handled as a special case. The limit is that it cannot check the second line's check digits or confirm the name is correct.
Pattern (JavaScript)
/^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$/Starts at the start of the line/string, then the character "P", then one of the characters A-Z<, then (an uppercase letter, exactly 3 times OR the text "D<<"), then one of the characters A-Z<, exactly 39 times, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid.
Examples
Matches
- ✓ P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<
- ✓ P<GBRTEST<<ALI<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- ✓ PDUTOSAMPLE<<JOHN<DOE<<<<<<<<<<<<<<<<<<<<<<<
Does not match
- ✗ P<UTOERIKSSON<<ANNA<MARIA
- ✗ I<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<
- ✗ P<utoeriksson<<anna<maria<<<<<<<<<<<<<<<<<<<
Try it
0 matches (JavaScript engine)
P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<
P<GBRTEST<<ALI<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
PDUTOSAMPLE<<JOHN<DOE<<<<<<<<<<<<<<<<<<<<<<<
P<UTOERIKSSON<<ANNA<MARIA
I<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<
P<utoeriksson<<anna<maria<<<<<<<<<<<<<<<<<<<Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$/g;
const text = "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$')
text = 'P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$/';
$text = 'P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$");
String text = "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$`)
text := "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$");
var text = "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$").unwrap();
let text = "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^P[A-Z<](?:[A-Z]{3}|D<<)[A-Z<]{39}$/
text = 'P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must the line be exactly 44 characters?
- The TD3 passport format defines each MRZ line as 44 characters, padded with < where the name is short. A shorter or longer line usually means OCR dropped or added characters, so rejecting it early is helpful.
- Does this pattern work for ID cards?
- No. ID cards use the TD1 or TD2 layouts, which start with I, A or C and have different line lengths. You need a separate pattern for those formats.
- How do I extract the surname and given names?
- After the line matches, take the characters from position 5 onward, split on the double filler <<, and replace single < characters with spaces. Trailing filler characters can then be trimmed.