Pakistan Special
Islamabad Landline Number (051) Regex
Islamabad and Rawalpindi share the area code 051, and local landline numbers there have seven digits. This pattern accepts the local form 051-1234567, the same number without the dash, and the international form with +92 in place of the leading zero, such as +92-51-1234567. It rejects numbers from other area codes and any local part that is not exactly seven digits. A focused pattern like this suits forms for a single city, such as a local delivery service or a directory of offices in the capital, where a general landline rule would be too loose to catch typing mistakes.
Pattern (JavaScript)
/^(?:\+92-?51|051)-?\d{7}$/Starts at the start of the line/string, then (the text "+92", then the character "-", optionally (zero or one time), then the text "51" OR the text "051"), then the character "-", optionally (zero or one time), then exactly 7 digits, then the end of the line/string.
Examples
Matches
- ✓ 051-1234567
- ✓ 0511234567
- ✓ +92-51-1234567
Does not match
- ✗ 042-1234567
- ✗ 051-123456
- ✗ 051-12345678
Try it
0 matches (JavaScript engine)
051-1234567
0511234567
+92-51-1234567
042-1234567
051-123456
051-12345678Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:\+92-?51|051)-?\d{7}$/g;
const text = "051-1234567";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:\+92-?51|051)-?\d{7}$')
text = '051-1234567'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:\+92-?51|051)-?\d{7}$/';
$text = '051-1234567';
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("^(?:\\+92-?51|051)-?\\d{7}$");
String text = "051-1234567";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:\+92-?51|051)-?\d{7}$`)
text := "051-1234567"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:\+92-?51|051)-?\d{7}$");
var text = "051-1234567";
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"^(?:\+92-?51|051)-?\d{7}$").unwrap();
let text = "051-1234567";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:\+92-?51|051)-?\d{7}$/
text = '051-1234567'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this cover Rawalpindi numbers too?
- Yes. Rawalpindi uses the same 051 area code as Islamabad, so their landline numbers share this shape.
- How do I adapt it for Lahore or Karachi?
- Replace 51 with 42 or 21 and change the local digit count to eight, since those cities use eight-digit local numbers.
- Will it accept spaces instead of dashes?
- No. Only optional dashes are allowed. Change each -? to [-\s]? if your data uses spaces.