Pakistan Special
Pakistan CNIC Without Dashes Regex
Databases, spreadsheets and many online forms store the CNIC as a plain run of thirteen digits, for example 1234512345671, because it is easier to index and compare than the dashed form. This pattern accepts exactly thirteen digits and nothing else, so a dash, a space or a missing digit makes the value fail. It is useful for checking stored records or the output of a normalisation step that strips dashes. Like the dashed version, it checks the 13-digit shape only; a regex cannot confirm that a CNIC is real or belongs to a person.
Pattern (JavaScript)
/^\d{13}$/Starts at the start of the line/string, then exactly 13 digits, then the end of the line/string.
Limit: Checks the 13-digit shape only; a regex cannot confirm that a CNIC is real or belongs to a person.
Examples
Matches
- ✓ 1234512345671
- ✓ 3520200000003
- ✓ 4210111111110
Does not match
- ✗ 12345-1234567-1
- ✗ 123456789012
- ✗ 12345123456712
Try it
0 matches (JavaScript engine)
1234512345671
3520200000003
4210111111110
12345-1234567-1
123456789012
12345123456712Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^\d{13}$/g;
const text = "1234512345671";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^\d{13}$')
text = '1234512345671'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^\d{13}$/';
$text = '1234512345671';
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("^\\d{13}$");
String text = "1234512345671";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\d{13}$`)
text := "1234512345671"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^\d{13}$");
var text = "1234512345671";
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"^\d{13}$").unwrap();
let text = "1234512345671";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^\d{13}$/
text = '1234512345671'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I convert the plain form back to the dashed layout?
- Replace ^(\d{5})(\d{7})(\d)$ with $1-$2-$3. The three groups become the familiar 12345-1234567-1 layout.
- Why store CNICs without dashes?
- One consistent format makes searching and duplicate checks reliable. Dashes can be added back when the number is shown to a person.
- Will this also match other 13-digit numbers?
- Yes. Any thirteen digits pass, because the shape is all a regex can check. Use the field label and context to know the value is meant to be a CNIC.