Identity
UK National Insurance Number Regex
A UK National Insurance number has two prefix letters, six digits and a final letter from A to D, often written in pairs such as AB 12 34 56 C. This pattern applies the published letter restrictions: the first letter cannot be D, F, I, Q, U or V, and the second cannot be D, F, I, O, Q, U or V. It suits payroll, pension and HR onboarding forms. One honest limit is that a few prefix pairs such as GB, NK and TN are also disallowed, and this lookaround-free pattern does not block them.
Pattern (JavaScript)
/^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$/Starts at the start of the line/string, then one of the characters A-CEGHJ-PR-TW-Z, then one of the characters A-CEGHJ-NPR-TW-Z, then a space, optionally (zero or one time), then a digit, exactly 2 times, then a space, optionally (zero or one time), then a digit, exactly 2 times, then a space, optionally (zero or one time), then a digit, exactly 2 times, then a space, optionally (zero or one time), then one of the characters A-D, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid.
Examples
Matches
- ✓ AB123456C
- ✓ CE 12 34 56 A
- ✓ JK987654D
Does not match
- ✗ DA123456C
- ✗ AB123456E
- ✗ AB12345C
Try it
0 matches (JavaScript engine)
AB123456C
CE 12 34 56 A
JK987654D
DA123456C
AB123456E
AB12345CCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$/g;
const text = "AB123456C";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$')
text = 'AB123456C'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$/';
$text = 'AB123456C';
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-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$");
String text = "AB123456C";
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-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$`)
text := "AB123456C"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$");
var text = "AB123456C";
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-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$").unwrap();
let text = "AB123456C";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[A-D]$/
text = 'AB123456C'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are some letters missing from the character classes?
- HMRC does not use D, F, I, Q, U or V in either prefix position, and O is also excluded as the second letter. Leaving them out of the classes rejects numbers that could never be issued.
- How do I block the disallowed prefixes like GB or ZZ?
- Add a negative lookahead at the start, such as (?!BG|GB|KN|NK|NT|TN|ZZ), in engines that support it. In Go or Rust, run a second check on the first two characters in code instead.
- Can the spaces appear in different places?
- The pattern allows an optional single space after the prefix, between each digit pair and before the suffix. Other spacing, such as a space inside a digit pair, is rejected, so normalise unusual input first.