Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Pattern

Regex for UK National Insurance Number

NI number — 2 letters + 6 digits + 1 letter.

The UK National Insurance Number regex is ^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$ — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$
Try in explainer → Download cheat sheet ↓

What it matches

  • AB123456C
  • JT987654A

What it doesn't match

  • BG123456C
  • 12345678
  • AB1234567

Notes & gotchas

Excludes invalid prefixes (BG, GB, NK, KN, TN, NT, ZZ) and letters D, F, I, O, Q, U, V from positions where they can't appear. Suffix is A, B, C, D, or sometimes absent.

Code in your language

Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.

Open in explainer →

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
^start of string (or line in multiline mode)
(?!start of a negative lookahead — NOT followed by
BGliteral text “BG”
|OR — try the alternative
GBliteral text “GB”
|OR — try the alternative
NKliteral text “NK”
|OR — try the alternative
KNliteral text “KN”
|OR — try the alternative
TNliteral text “TN”
|OR — try the alternative
NTliteral text “NT”
|OR — try the alternative
ZZliteral text “ZZ”
)end of group
[A-CEGHJ-PR-TW-Z]any of: A–C, “E”, “G”, “H”, J–P, R–T, W–Z
[A-CEGHJ-NPR-TW-Z]any of: A–C, “E”, “G”, “H”, J–N, “P”, R–T, W–Z
\d{6}exactly 6 times: digit (0–9)
[A-D]?optional (zero or one): any of: A–D
$end of string (or line in multiline mode)

About this pattern

UK identifier formats are set by HMRC, Royal Mail, and the BACS payments scheme. The regex here matches the format; full validation may need an additional checksum or lookup.

Quick usage in different languages

This exact pattern — with the correct escaping and idioms for each language:

  • JavaScript: /^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$/.test(value)
  • Python: re.match(r"^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$", value)
  • Java: Pattern.compile("^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\\d{6}[A-D]?$").matcher(value).matches()
  • C# / .NET: Regex.IsMatch(value, @"^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$")
  • Go: regexp.MustCompile(`^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$`).MatchString(value)
  • Ruby: /^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$/.match?(value)
  • PHP: preg_match('~^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[A-D]?$~', $value)

The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.

Common pitfalls

  • Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
  • Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
  • Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the uk national insurance number against a source of truth (database, API, or checksum) where it matters.

Standards & sources

This pattern is based on the following authoritative specification(s) and issuing authorities. Formats can change — always confirm against the primary source.

Related patterns

More patterns in the United Kingdom 🇬🇧 category:

See also

Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.


Want more patterns? Browse the full library →