Fix UK_NINO regex matching a numeric suffix character#2112
Merged
SharonHart merged 2 commits intoJun 29, 2026
Merged
Conversation
The suffix capture group was written as `([a-dA-D{1}])`, placing the
intended `{1}` quantifier *inside* the character class. Inside a class,
`{`, `1`, and `}` are treated as literal members, so the class admitted
the digit `1` as a valid NINO suffix (the `{`/`}` variants are only
blocked incidentally by the trailing `\b`).
A UK National Insurance Number always ends in a single suffix letter
A, B, C or D (HMRC/DWP format); a numeric suffix is never valid. As a
result inputs such as `AB 12 34 56 1` were wrongly reported as a
UK_NINO. The prefix groups in the same pattern already use the correct
`[...]{1}` form (quantifier outside the class).
Fix by moving the quantifier outside the class: `([a-dA-D]{1})`.
Adds parametrized cases asserting a numeric suffix is rejected.
SharonHart
approved these changes
Jun 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
UK_NINO(UK National Insurance Number) pattern accepts a numeric suffix, producing false positives such asAB 12 34 56 1.The suffix capture group is written as:
The intended quantifier
{1}was placed inside the character class. Inside a class,{,1and}are literal members, so the class is effectively[a-dA-D{1}]— it admits the digit1in addition to the valid suffix letters. ({/}are only blocked incidentally by the trailing\b, but1is a word character and is not.)A NINO always ends in a single suffix letter A, B, C or D (HMRC/DWP format); a numeric suffix is never valid. The two prefix groups in the very same pattern already use the correct form with the quantifier outside the class (
[...]{1}).Fix
Move the quantifier outside the class:
Tests
Added parametrized cases asserting a numeric suffix is rejected (
AB 12 34 56 1,ab1234561). All existingtest_uk_nino_recognizer.pycases continue to pass (12 passed).ruff check .is clean.