Fix UK_NINO regex matching numeric suffix character (#2112)

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.

Co-authored-by: Sharon Hart <sharonh.dev@gmail.com>
This commit is contained in:
jichaowang02-lang
2026-06-29 16:14:55 +01:00
committed by GitHub
parent 640d8d7a6d
commit 0ff7541b3e
2 changed files with 4 additions and 1 deletions

View File

@@ -18,7 +18,7 @@ class UkNinoRecognizer(PatternRecognizer):
PATTERNS = [
Pattern(
"NINO (medium)",
r"\b(?!bg|gb|nk|kn|nt|tn|zz|BG|GB|NK|KN|NT|TN|ZZ) ?([a-ceghj-pr-tw-zA-CEGHJ-PR-TW-Z]{1}[a-ceghj-npr-tw-zA-CEGHJ-NPR-TW-Z]{1}) ?([0-9]{2}) ?([0-9]{2}) ?([0-9]{2}) ?([a-dA-D{1}])\b", # noqa: E501
r"\b(?!bg|gb|nk|kn|nt|tn|zz|BG|GB|NK|KN|NT|TN|ZZ) ?([a-ceghj-pr-tw-zA-CEGHJ-PR-TW-Z]{1}[a-ceghj-npr-tw-zA-CEGHJ-NPR-TW-Z]{1}) ?([0-9]{2}) ?([0-9]{2}) ?([0-9]{2}) ?([a-dA-D]{1})\b", # noqa: E501
0.5,
),
]

View File

@@ -26,6 +26,9 @@ def entities():
("Here is my National Insurance Number YZ 61 48 68 B", 1, ((36, 50),), ((0.5, 0.5),), ),
# Invalid National Insurance Numbers
("AA 12 34 56 H", 0, (), (), ),
# The suffix must be a letter A-D; a numeric suffix is never valid.
("AB 12 34 56 1", 0, (), (), ),
("ab1234561", 0, (), (), ),
("FQ 00 00 00 C", 0, (), (), ),
("BG123612A", 0, (), (), ),
("nino: nt 99 88 77 a", 0, (), (), ),