Pakistan Special
Pakistan NTN Shape Regex
Businesses in Pakistan often quote a National Tax Number on invoices and quotations, commonly written as seven digits followed by a dash and a single digit, for example 1234567-8. This pattern accepts exactly that layout and rejects values with missing digits, extra digits or no dash. It is a lightweight check for supplier forms and invoice templates, catching typing mistakes before a record is saved. The pattern covers the common seven-plus-one shape only and does not compute the final digit. It also says nothing about whether a business is registered or active, which only the tax authority can confirm.
Pattern (JavaScript)
/^\d{7}-\d$/Starts at the start of the line/string, then exactly 7 digits, then the character "-", then a digit (0-9), then the end of the line/string.
Examples
Matches
- ✓ 1234567-8
- ✓ 0000000-1
- ✓ 7654321-0
Does not match
- ✗ 12345678
- ✗ 123456-7
- ✗ 1234567-89
Try it
0 matches (JavaScript engine)
1234567-8
0000000-1
7654321-0
12345678
123456-7
1234567-89Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^\d{7}-\d$/g;
const text = "1234567-8";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^\d{7}-\d$')
text = '1234567-8'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^\d{7}-\d$/';
$text = '1234567-8';
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{7}-\\d$");
String text = "1234567-8";
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{7}-\d$`)
text := "1234567-8"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^\d{7}-\d$");
var text = "1234567-8";
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{7}-\d$").unwrap();
let text = "1234567-8";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^\d{7}-\d$/
text = '1234567-8'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does the pattern check the final digit?
- No. It only checks that a single digit follows the dash. Any arithmetic on that digit has to happen in code.
- How do I accept the number without a dash?
- Use ^\d{7}-?\d$ to make the dash optional. Store the value in one form so lookups stay consistent.
- Can it tell whether a business is a registered taxpayer?
- No. A regex sees only the characters typed. Registration status can only come from the tax authority's own records.