Pakistan Special
Pakistan Landline Number Regex
Pakistani landline numbers combine an area code that starts with 0, such as 021 for Karachi, 042 for Lahore or 051 for Islamabad, with a local subscriber number. Area codes in smaller towns are longer, and the local number gets shorter to match, so the total length varies. This pattern accepts a two to four digit code after the leading zero, a dash, and five to eight local digits. The second digit cannot be 3, which keeps mobile numbers such as 0300-0000000 from passing as landlines. Because lengths vary by area, this covers the common shapes and checks the layout only.
Pattern (JavaScript)
/^0[124-9]\d{1,3}-\d{5,8}$/Starts at the start of the line/string, then the character "0", then one of the characters 124-9, then a digit (0-9), between 1 and 3 times, then the character "-", then a digit (0-9), between 5 and 8 times, then the end of the line/string.
Examples
Matches
- ✓ 021-34567890
- ✓ 042-35000000
- ✓ 051-1234567
Does not match
- ✗ 0300-0000000
- ✗ 21-34567890
- ✗ 021-123
Try it
0 matches (JavaScript engine)
021-34567890
042-35000000
051-1234567
0300-0000000
21-34567890
021-123Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^0[124-9]\d{1,3}-\d{5,8}$/g;
const text = "021-34567890";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^0[124-9]\d{1,3}-\d{5,8}$')
text = '021-34567890'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^0[124-9]\d{1,3}-\d{5,8}$/';
$text = '021-34567890';
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[124-9]\\d{1,3}-\\d{5,8}$");
String text = "021-34567890";
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[124-9]\d{1,3}-\d{5,8}$`)
text := "021-34567890"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^0[124-9]\d{1,3}-\d{5,8}$");
var text = "021-34567890";
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[124-9]\d{1,3}-\d{5,8}$").unwrap();
let text = "021-34567890";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^0[124-9]\d{1,3}-\d{5,8}$/
text = '021-34567890'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must the number include a dash?
- Without a separator it is hard to tell where a variable-length area code ends. Requiring the dash keeps the two parts unambiguous.
- Why is the second digit not allowed to be 3?
- Numbers starting with 03 are mobile numbers in Pakistan. Excluding 3 keeps the landline and mobile patterns from overlapping.
- Does it accept numbers with +92 instead of 0?
- No. It covers the local form. Replace +92 with 0 before checking, or add an alternative prefix group.