UK regexes¶
Python regexes for UK identifiers, with checksum validation: National Insurance number, NHS number, UTR, VAT, sort code, postcode, plates and phones.
22 regexes, from regular_regexes/gb/regexes.json, called as rre.gb.<name>(text).
Personal numbers (2) · Identity documents (3) · Companies and tax (4) · Banking and payments (3) · Phones and SIM cards (6) · Places and addresses (2) · Vehicles, ships and parcels (2)
Personal numbers¶
| function | checksum validator |
|---|---|
rre.gb.national_insurance_number |
|
rre.gb.nhs_number |
gb_nhs_number |
national_insurance_number¶
UK National Insurance number (NINO, NI number): two prefix letters as HMRC issues them, six digits and a suffix A to D, with or without spaces, like AB 12 34 56 C.
rre.gb.national_insurance_number(text)
\b(?!BG|GB|KN|NK|NT|TN|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?\d{2} ?\d{2} ?\d{2} ?[A-D]\b
nhs_number¶
NHS number after NHS, NHS No or NHS Number, keyword included in the match: ten digits, together or grouped 3-3-4 with spaces or dashes, like NHS Number: 999 000 0018.
rre.gb.nhs_number(text)
(?i)\bNHS\s?(?:number|no\.?)?\s?:?\s?\d{3}[ -]?\d{3}[ -]?\d{4}\b
Matches are checked with gb_nhs_number; one that fails it is dropped.
Identity documents¶
| function | checksum validator |
|---|---|
rre.gb.driving_licence_number |
|
rre.gb.mrz_passport |
mrz_td3 |
rre.gb.passport_number |
driving_licence_number¶
UK driving licence number (DVLA driver number): 16 characters from the surname, date of birth and initials, like MORGA657054SM9IJ or grouped as MORGA 657054 SM 9IJ.
rre.gb.driving_licence_number(text)
\b[A-Z][A-Z9]{4} ?\d(?:[05][1-9]|[16][0-2])(?:0[1-9]|[12]\d|3[01])\d ?[A-Z][A-Z9] ?\d[A-Z]{2}\b
mrz_passport¶
Machine-readable zone (MRZ) of a British passport, second line (ICAO TD3): 44 characters with the passport number, GBR or another British code (GBN, GBD, ...) as nationality, birth date, sex and expiry date.
rre.gb.mrz_passport(text)
(?<![A-Z0-9<])[A-Z0-9<]{9}\dGB[RDNOPS]\d{6}\d[MFX<]\d{6}\d[A-Z0-9<]{14}[\d<]\d(?![A-Z0-9<])
Matches are checked with mrz_td3; one that fails it is dropped.
passport_number¶
UK passport number after passport, passport no or passport number, keyword included in the match: nine digits, like Passport No: 123456789.
rre.gb.passport_number(text)
(?i)\bpassport\s?(?:number|no\.?)?\s?:?\s?\d{9}\b
Companies and tax¶
| function | checksum validator |
|---|---|
rre.gb.company_name |
|
rre.gb.company_number |
|
rre.gb.utr |
gb_utr |
rre.gb.vat_number |
gb_vat_number |
company_name¶
UK company name: up to six capitalized words on one line followed by Ltd, Limited, PLC, LLP or CIC in any case, with an optional dot, like Acme Widgets Ltd. or Example Holdings PLC.
rre.gb.company_name(text)
(?<![\w&'.-])[A-Z][\w&'.-]*(?: (?:[A-Z0-9][\w&'.-]*|&)){0,5} (?i:limited|ltd|plc|llp|cic)\b\.?
company_number¶
UK company number (Companies House registration number, CRN) after company no, registered in England and Wales No. or CRN, keyword included: 6 to 8 digits, or SC, NI or OC and 6 digits.
rre.gb.company_number(text)
(?i)\b(?:company\s(?:reg(?:istration)?\.?\s)?(?:number|no\.?)|registered\s(?:in\s(?:England(?:\s(?:and|&)\sWales)?|Wales|Scotland|Northern\sIreland),?\s)?(?:number|no\.?)|CRN)\s?:?\s?(?:[A-Z]{2}\d{6}|\d{6,8})\b
utr¶
UK Unique Taxpayer Reference (UTR) after UTR, Unique Taxpayer Reference or tax reference, keyword included: ten digits, together or split 5-5, like UTR 22345 67890.
rre.gb.utr(text)
(?i)\b(?:UTR|unique\staxpayer\sreference|tax\sreference)(?:\s?(?:number|no\.?))?\s?:?\s?\d{5}\s?\d{5}\b
Matches are checked with gb_utr; one that fails it is dropped.
vat_number¶
UK VAT registration number after GB, XI (Northern Ireland) or VAT No, which is part of the match: nine digits like GB999 9999 73, twelve with a branch, or government GBGD001 and health GBHA500.
rre.gb.vat_number(text)
(?:\b(?:GB|XI) ?|(?i:\bVAT\s?(?:reg(?:istration)?\.?\s?)?(?:number|no\.?)\s?:?\s?))\d{3}[ .-]?\d{4}[ .-]?\d{2}(?:\d{3})?\b|\bGB ?(?:GD[0-4]|HA[5-9])\d{2}\b
Matches are checked with gb_vat_number; one that fails it is dropped.
Banking and payments¶
| function | checksum validator |
|---|---|
rre.gb.bic |
|
rre.gb.iban |
iban |
rre.gb.sort_code |
bic¶
BIC (SWIFT code) of a UK bank: four letters, GB and two letters or digits, with an optional three-character branch code, like NWBKGB2L or BARCGB22XXX.
rre.gb.bic(text)
\b[A-Z]{4}GB[A-Z0-9]{2}(?:[A-Z0-9]{3})?\b
iban¶
UK IBAN: GB, two check digits, a four-letter bank code and 14 digits (sort code and account number), with or without spaces, like GB29 NWBK 6016 1331 9268 19.
rre.gb.iban(text)
\bGB\d{2} ?[A-Z]{4}(?: ?\d){14}\b
Matches are checked with iban; one that fails it is dropped.
sort_code¶
UK bank sort code after sort code or s/c, keyword included, with the 8-digit account number when it follows; without a keyword only when written 60-16-13 31926819.
rre.gb.sort_code(text)
(?i)\b(?:sort(?:\s?code)?|s/c)\W{0,3}\d{2}[-\s]?\d{2}[-\s]?\d{2}\b(?:\W{1,3}(?:a/c|acc(?:ount)?(?:\s?(?:no\.?|number))?)\W{0,3}\d{8}\b)?|\b\d{2}-\d{2}-\d{2}[\s,/]{1,3}\d{8}\b
Phones and SIM cards¶
| function | checksum validator |
|---|---|
rre.gb.iccid |
luhn |
rre.gb.imsi |
|
rre.gb.mobile_number |
phone |
rre.gb.msisdn |
phone |
rre.gb.phone_number |
phone |
rre.gb.premium_number |
phone |
iccid¶
SIM card number (ICCID) of a UK SIM: 8944 followed by 14 to 16 digits, without spaces, like 89441000000000000018.
rre.gb.iccid(text)
\b8944\d{14,16}\b
Matches are checked with luhn; one that fails it is dropped.
imsi¶
IMSI (subscriber identity on a SIM) of a UK network: mobile country code 234 or 235 and 12 more digits, 15 in all, like 234150123456789.
rre.gb.imsi(text)
\b23[45]\d{12}\b
mobile_number¶
UK mobile number: 07 and nine more digits, national or with +44, 0044 or +44 (0), with spaces or dashes, like 07400 123456; only valid UK, Channel Islands and Isle of Man mobiles.
rre.gb.mobile_number(text)
(?:(?:\+|\b00)44 ?(?:\(0\) ?|0)?|\b0)7(?=[1-57-9]|624)(?:[ -]?\d){9}\b
Matches are checked with phone (regions GB GG JE IM, kind mobile); one that fails it is dropped.
msisdn¶
UK mobile number in MSISDN form, as in telecom records: 44 and the number without its 0 or a + in front, 12 digits like 447400123456; only valid UK, Channel Islands and Isle of Man mobiles.
rre.gb.msisdn(text)
(?<!\+)\b447(?:[1-57-9]\d{8}|624\d{6})\b
Matches are checked with phone (regions GB GG JE IM, kind msisdn); one that fails it is dropped.
phone_number¶
UK phone number with a ten-digit national number: 0, +44, 0044 or +44 (0), or the area code in brackets like (01234) 567890, with spaces or dashes; only valid UK numbers.
rre.gb.phone_number(text)
(?:(?:\+|\b00)44 ?(?:\(0\) ?|0)?|\(?\b0)[1-9]\d(?:[ )-]{0,2}\d){8}\b
Matches are checked with phone (regions GB GG JE IM); one that fails it is dropped.
premium_number¶
UK premium-rate number: 090, 091 or 098 and eight more digits, national or with +44, like 0909 879 0123; only valid UK numbers.
rre.gb.premium_number(text)
(?:(?:\+|\b00)44 ?(?:\(0\) ?|0)?|\b0)9[018](?:[ -]?\d){8}\b
Matches are checked with phone (regions GB GG JE IM, kind premium); one that fails it is dropped.
Places and addresses¶
| function | checksum validator |
|---|---|
rre.gb.postal_code |
|
rre.gb.street_address |
postal_code¶
UK postcode with a real postcode area (Channel Islands and Isle of Man included), with or without the space, like SW1A 1AA, M1 1AE or EC1A1BB.
rre.gb.postal_code(text)
\b(?:A[BL]|B[ABDFHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d[A-Z\d]? ?\d[ABD-HJLNP-UW-Z]{2}\b
street_address¶
UK street address, number first: house number (12a, 221B, not a year like 2019) and street with a common type (Street, St, Road, Lane, Avenue, Drive, Close, ...) on one line, with Flat or Unit before it, like 221B Baker Street.
rre.gb.street_address(text)
\b(?:(?:Flat|Apartment|Apt|Unit) \d+[A-Z]?,? )?(?!(?:19|20)\d\d(?!\d))\d{1,4}[A-Za-z]? (?:[A-Z][a-z'-]*\.? ){1,4}(?:Street|St\b(?!\.? [A-Z])|Road|Rd|Lane|Avenue|Ave|Drive|Close|Way|Place|Crescent|Court|Gardens|Grove|Terrace|Square|Mews|Hill|Row|Walk|Parade)\b
Vehicles, ships and parcels¶
| function | checksum validator |
|---|---|
rre.gb.aircraft_registration |
|
rre.gb.license_plate |
aircraft_registration¶
UK civil aircraft registration: G- followed by four letters, like G-ABCD or G-EZAB.
rre.gb.aircraft_registration(text)
\bG-[A-Z]{4}\b
license_plate¶
UK number plate (vehicle registration number): the current BD51 SMR with real area letters, and the older prefix P123 ABC and suffix ABC 123D styles, with or without the space.
rre.gb.license_plate(text)
\b(?:[A-HK-PRSV-Y][A-HJ-PR-Y]\d{2} ?[A-HJ-PR-Z]{3}|[A-HJ-NPR-TV-Y](?:[1-9]\d\d|[3-9]\d|2[1-9]) ?[A-HJ-PR-Y]{3}|[A-HJ-PR-Y]{3} ?[1-9]\d{1,2}[A-HJ-NPR-TV-Y])\b