Identity
Spanish DNI or NIE Shape Regex
Spanish citizens hold a DNI made of eight digits plus a control letter, while foreign residents receive an NIE that starts with X, Y or Z, followed by seven digits and a control letter. This pattern accepts both shapes and restricts the control letter to the set actually used, which excludes I, O and U. It suits rental contracts, employment forms, bank onboarding and delivery services in Spain. The honest limit is that the control letter is calculated from the number using a modulo 23 table, and a regex cannot check that calculation.
Pattern (JavaScript)
/^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$/Starts at the start of the line/string, then (a digit, exactly 8 times OR one of the characters XYZ, then a digit, exactly 7 times), then one of the characters A-HJ-NP-TV-Z, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid.
Examples
Matches
- ✓ 12345678Z
- ✓ X1234567L
- ✓ Y7654321G
Does not match
- ✗ 1234567Z
- ✗ 12345678I
- ✗ A1234567L
Try it
0 matches (JavaScript engine)
12345678Z
X1234567L
Y7654321G
1234567Z
12345678I
A1234567LCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$/g;
const text = "12345678Z";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$')
text = '12345678Z'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$/';
$text = '12345678Z';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$");
String text = "12345678Z";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$`)
text := "12345678Z"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$");
var text = "12345678Z";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$").unwrap();
let text = "12345678Z";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[0-9]{8}|[XYZ][0-9]{7})[A-HJ-NP-TV-Z]$/
text = '12345678Z'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are the letters I, O and U excluded?
- The official control-letter table uses 23 letters and leaves out I, O and U to avoid confusion with digits and other letters. Excluding them catches some typing mistakes immediately.
- How is the control letter calculated?
- For a DNI, divide the eight-digit number by 23 and use the remainder as an index into the string TRWAGMYFPDXBNJZSQVHLCKE. For an NIE, first replace X, Y or Z with 0, 1 or 2 and then apply the same rule.
- Should I accept a dash before the letter?
- People sometimes write 12345678-Z. Remove dashes and spaces before testing, so the stored value stays consistent.