Skip to content

Spanish regexes

Python regexes for Spanish identifiers, with checksum validation: DNI, NIE, CIF, NIF-IVA, Seguridad Social, IBAN, passport, matrícula and phone numbers.

22 regexes, from regular_regexes/es/regexes.json, called as rre.es.<name>(text).

Personal numbers (3) · Identity documents (3) · Companies and tax (3) · Banking and payments (2) · Phones and SIM cards (6) · Places and addresses (2) · Vehicles, ships and parcels (2) · Justice and police (1)

Personal numbers

function checksum validator
rre.es.dni es_nif
rre.es.nie es_nif
rre.es.social_security_number es_nss

dni

Spanish national identity number (DNI, documento nacional de identidad): eight digits and a control letter, like 12345678Z, 12345678-Z or 12.345.678-Z.

rre.es.dni(text)

\b\d{2}\.?\d{3}\.?\d{3}-?[TRWAGMYFPDXBNJZSQVHLCKE]\b

Matches are checked with es_nif; one that fails it is dropped.

nie

Spanish foreigner identity number (NIE, número de identidad de extranjero): X, Y or Z, seven digits and a control letter, like X1234567L or Y-1234567-X.

rre.es.nie(text)

\b[XYZ]-?\d{7}-?[TRWAGMYFPDXBNJZSQVHLCKE]\b

Matches are checked with es_nif; one that fails it is dropped.

social_security_number

Spanish social security number (NSS, número de la Seguridad Social, NAF): province 01 to 53 or 66, eight digits and two control digits, like 28/12345678/40, or the old 11-digit form.

rre.es.social_security_number(text)

\b(?:0[1-9]|[1-4]\d|5[0-3]|66)(?:[ /-]?\d{8}[ /-]?|[ /]\d{7}[ /])\d{2}\b

Matches are checked with es_nss; one that fails it is dropped.

Identity documents

function checksum validator
rre.es.mrz_id_card mrz_td1
rre.es.mrz_passport mrz_td3
rre.es.passport_number

mrz_id_card

Machine-readable zone (MRZ) of a Spanish identity card (DNI), first line (ICAO TD1): 30 characters with ESP as issuing country, like IDESPBAA000589599999999R<<<<<<.

rre.es.mrz_id_card(text)

(?<![A-Z0-9<])[IAC][A-Z<]ESP[A-Z0-9<]{25}(?![A-Z0-9<])

Matches are checked with mrz_td1; one that fails it is dropped.

mrz_passport

Machine-readable zone (MRZ) of a Spanish passport, second line (ICAO TD3): 44 characters with the passport number, ESP as nationality, date of birth, sex and expiry date.

rre.es.mrz_passport(text)

(?<![A-Z0-9<])[A-Z0-9<]{9}\dESP\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

Spanish passport number (número de pasaporte): three capital letters and six digits, like PAA123456 or XDA654321.

rre.es.passport_number(text)

\b[A-Z]{3}\d{6}\b

Companies and tax

function checksum validator
rre.es.cif es_nif
rre.es.company_name
rre.es.vat_number es_nif

cif

Spanish company tax code (CIF, código de identificación fiscal): a letter for the type of entity, seven digits and a control digit or letter, like B12345674 or B-12.345.67-4; not as a segment of a URL path.

rre.es.cif(text)

(?<!/)\b[ABCDEFGHJNPQRSUVW]-?\d{2}\.?\d{3}\.?\d{2}-?[0-9A-J]\b

Matches are checked with es_nif; one that fails it is dropped.

company_name

Spanish company name (razón social): capitalized words, with de, del, la or y between them, followed by S.A., S.L., S.L.U., S.A.U., S.L.L., S.L.N.E., S.A.T., S.Coop. or A.I.E., like Telefónica, S.A..

rre.es.company_name(text)

\b(?!(?:Ayer|Hoy|Según|Para|Con|Contra|Por|Desde|Hasta|Entre|Sobre|Tras|Ante|Sin|Pero|También|Además|Que|Como|Cuando|En)\s)[A-ZÁÉÍÓÚÑ][\w&'.-]{0,40}(?:\s(?:(?:de|del|la|las|los|y)\s)?(?:[A-ZÁÉÍÓÚÑ0-9][\w&'.-]{0,40}|&)){0,6}?,?\s(?:S(?:\.\s?)?(?:A\.?(?:[ULT]\.?)?|L\.?(?:[UL]\.?|N\.?E\.?)?|Coop\.?|C(?:om)?\.?)|A\.?I\.?E\.?)(?!\w)

vat_number

Spanish VAT number (NIF-IVA): ES followed by the NIF, which is a DNI, NIE or CIF, like ESB12345674, ES12345678Z or ESX1234567L.

rre.es.vat_number(text)

\bES[ -]?[0-9A-HJ-NP-SU-Z]\d{7}[0-9A-Z]\b

Matches are checked with es_nif; one that fails it is dropped.

Banking and payments

function checksum validator
rre.es.bic
rre.es.iban iban

bic

BIC (SWIFT code) of a Spanish bank: four letters, ES and two letters or digits, with an optional three-character branch code, like CAIXESBBXXX or BSCHESMM.

rre.es.bic(text)

\b[A-Z]{4}ES[A-Z0-9]{2}(?:[A-Z0-9]{3})?\b

iban

Spanish IBAN: ES, two check digits and 20 digits, with or without spaces, in groups of four or as ES91 2100 0418 45 0200051332.

rre.es.iban(text)

\bES ?\d{2}(?: ?\d){20}\b

Matches are checked with iban; one that fails it is dropped.

Phones and SIM cards

function checksum validator
rre.es.iccid luhn
rre.es.imsi
rre.es.mobile_number phone
rre.es.msisdn phone
rre.es.phone_number phone
rre.es.premium_number phone

iccid

SIM card number (ICCID) of a Spanish SIM: 8934 followed by 14 to 16 digits, without spaces, like 8934011234567890128.

rre.es.iccid(text)

\b8934\d{14,16}\b

Matches are checked with luhn; one that fails it is dropped.

imsi

IMSI (subscriber identity on a SIM) of a Spanish network: mobile country code 214 and 12 more digits, 15 in all, like 214011234567890.

rre.es.imsi(text)

\b214\d{12}\b

mobile_number

Spanish mobile number (móvil): nine digits starting with 6 or 7, grouped 3-3-3, 3-2-2-2 or 2-3-2-2, with or without +34 or 0034; only valid Spanish mobile numbers.

rre.es.mobile_number(text)

(?:(?:\+|\b00)34[\s.-]?|(?<![\w+])(?<!\d[\s./-])(?<!\(0\) )(?:(?<!\d\) )|(?<=34\) )))(?:6\d|7[1-9])(?:\d(?:[\s.-]?\d{3}){2}|[\s.-]?\d{3}(?:[\s.-]?\d{2}){2}|\d(?:[\s.-]?\d{2}){3})(?![\s.-]?\d)

Matches are checked with phone (regions ES, kind mobile); one that fails it is dropped.

msisdn

Spanish mobile number in MSISDN form, as in telecom records: 34 and the nine-digit number, 11 digits like 34612345678; only valid Spanish mobile numbers.

rre.es.msisdn(text)

(?<!\+)\b34(?:6\d|7[1-9])\d{7}\b

Matches are checked with phone (regions ES, kind msisdn); one that fails it is dropped.

phone_number

Spanish phone number (número de teléfono), landline or mobile: nine digits starting with 6 to 9, in the usual groupings, with or without +34 or 0034; only valid Spanish numbers.

rre.es.phone_number(text)

(?:(?:\+|\b00)34[\s.-]?|(?<![\w+])(?<!\d[\s./-])(?<!\(0\) )(?:(?<!\d\) )|(?<=34\) )))[6-9]\d(?:\d(?:[\s.-]?\d{3}){2}|[\s.-]?\d{3}(?:[\s.-]?\d{2}){2}|\d(?:[\s.-]?\d{2}){3})(?![\s.-]?\d)

Matches are checked with phone (regions ES); one that fails it is dropped.

premium_number

Spanish premium-rate (tarificación adicional: 803, 806, 807) or shared-cost number (901, 902): nine digits, with or without +34 or 0034, like 803 123 456 or 806 12 34 56; only valid Spanish numbers.

rre.es.premium_number(text)

(?:(?:\+|\b00)34[\s.-]?|(?<![\w+])(?<!\d[\s./-]))(?:80[367]|90[12])(?:(?:[\s.-]?\d{3}){2}|(?:[\s.-]?\d{2}){3})(?![\s.-]?\d)

Matches are checked with phone (regions ES, kind premium); one that fails it is dropped.

Places and addresses

function checksum validator
rre.es.postal_code_city
rre.es.street_address

postal_code_city

Spanish postal code (código postal) and town: five digits starting with the province, 01 to 52, and the place name, like 28013 Madrid or 28801 Alcalá de Henares (Madrid).

rre.es.postal_code_city(text)

\b(?:0[1-9]|[1-4]\d|5[0-2])\d{3}\s(?!EUR|Euro)[A-ZÁÉÍÓÚÑ][\w'·-]*(?:\s(?:(?:de|del|la|las|los|el|y|i)\s|[dl]')*[A-ZÁÉÍÓÚÑ][\w'·-]*)*(?:\s\([A-ZÁÉÍÓÚÑ][\w' ]{1,30}\))?

street_address

Spanish street address (dirección): Calle, C/, Avenida, Avda., Plaza, Paseo, Carretera, Camino, Ronda, Carrer or Rúa, also in lower case, the capitalized name and number or s/n, with nº and floor, like Calle Mayor 12, 3º B.

rre.es.street_address(text)

\b(?:(?i:Calle|Avenida|Plaza|Plaça|Paseo|Passeig|Carretera|Camino|Ronda|Travesía|Carrer|Rúa)\s|(?i:C/|Avda\.|Av\.|Pza\.|P\.?º|Ctra\.)\s?)(?:(?:de|del|la|las|los|el|do|da|dos|das)\s|[dl]')*(?:\d{1,2}[ºª]?\sde\s)?[A-ZÁÉÍÓÚÑ][\w'ºª. -]{0,40}?,?\s(?:[nN]\.?º\s?|núm\.\s?)?(?:\d{1,4}[A-Z]?|s/n)\b(?:,?\s\d{1,2}[º°](?:\s?(?:[A-Z]|\d{1,2}ª)\b)?)?

Vehicles, ships and parcels

function checksum validator
rre.es.aircraft_registration
rre.es.license_plate

aircraft_registration

Spanish aircraft registration (matrícula de aeronave): EC- followed by three letters, like EC-XYZ.

rre.es.aircraft_registration(text)

\bEC-[A-Z]{3}\b

license_plate

Spanish license plate (matrícula): four digits and three consonants, like 1234 BCD, or the older provincial plates with their dashes or spaces, like M-1234-AB; currency codes such as 5000 GBP are skipped.

rre.es.license_plate(text)

\b(?:\d{4}[ -]?(?!BTC|CHF|CNY|CZK|DKK|GBP|JPY|LTC|MXN|PLN|TRX|XMR|XRP)[B-DF-HJ-NPR-TV-Z]{3}|(?:A[BLV]?|B[AIU]?|C[ACEORSU]?|G[CEIRU]|HU?|IB|J|L[EOU]?|M[ALU]?|NA|O[RU]?|P[MO]?|S[AEGOS]?|T[EFO]?|V[AI]?|ZA?)([ -])\d{4}\1[A-Z]{1,2})\b

Justice and police

function checksum validator
rre.es.ecli

ecli

European Case Law Identifier (ECLI) of a Spanish court decision: ECLI:ES:, the court, the year and the number, like ECLI:ES:TS:2015:1234A.

rre.es.ecli(text)

\bECLI:ES:[A-Z]+:\d{4}:[A-Z0-9.]*[A-Z0-9]\b