Pakistan Special
Pakistan Passport Number Shape Regex
Pakistani passport numbers are commonly made of two capital letters followed by seven digits, as in AB1234567. Travel agencies, visa forms and hotel check-in systems often ask for this number, and a quick shape check catches typing errors such as a missing digit or a lowercase letter. This pattern accepts exactly two uppercase letters and seven digits with no spaces. It covers the common shape only and does not prove that a passport exists, is valid or belongs to the traveller. If you also accept older or official passports, test sample numbers and widen the pattern where needed.
Pattern (JavaScript)
/^[A-Z]{2}\d{7}$/Starts at the start of the line/string, then an uppercase letter, exactly 2 times, then exactly 7 digits, then the end of the line/string.
Examples
Matches
- ✓ AB1234567
- ✓ XY0000000
- ✓ QA7654321
Does not match
- ✗ A1234567
- ✗ AB123456
- ✗ ab1234567
Try it
0 matches (JavaScript engine)
AB1234567
XY0000000
QA7654321
A1234567
AB123456
ab1234567Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-Z]{2}\d{7}$/g;
const text = "AB1234567";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-Z]{2}\d{7}$')
text = 'AB1234567'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-Z]{2}\d{7}$/';
$text = 'AB1234567';
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("^[A-Z]{2}\\d{7}$");
String text = "AB1234567";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Z]{2}\d{7}$`)
text := "AB1234567"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-Z]{2}\d{7}$");
var text = "AB1234567";
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"^[A-Z]{2}\d{7}$").unwrap();
let text = "AB1234567";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-Z]{2}\d{7}$/
text = 'AB1234567'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are lowercase letters rejected?
- Passport numbers are printed in capitals. Convert input to uppercase before testing so users are not blocked by a case mistake.
- Can this confirm a passport is valid for travel?
- No. It only checks the shape of the number. Expiry dates and validity must be checked against the document itself or an official service.
- Does it read the machine-readable zone at the bottom of the passport?
- No. The machine-readable zone has its own fixed layout with check digits. Parse that separately if you scan passports.