Pakistan Special
Pakistan Mobile Number Regex
Pakistani mobile numbers written for local use start with 03, followed by two more digits for the network prefix and seven subscriber digits, giving eleven digits in total. People commonly write them as 0300-0000000 with a dash after the prefix, or as one unbroken run like 03000000000. This pattern accepts both forms, anchored at both ends, and rejects landline numbers, numbers with the wrong digit count and international forms with +92. It is a quick front-end check for contact forms and delivery addresses. It checks the shape only and cannot tell whether a number is active or which network currently serves it.
Pattern (JavaScript)
/^03\d{2}-?\d{7}$/Starts at the start of the line/string, then the text "03", then exactly 2 digits, then the character "-", optionally (zero or one time), then exactly 7 digits, then the end of the line/string.
Examples
Matches
- ✓ 0300-0000000
- ✓ 03000000000
- ✓ 0345-1234567
Does not match
- ✗ +923000000000
- ✗ 0300-000000
- ✗ 0400-0000000
Try it
0 matches (JavaScript engine)
0300-0000000
03000000000
0345-1234567
+923000000000
0300-000000
0400-0000000Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^03\d{2}-?\d{7}$/g;
const text = "0300-0000000";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^03\d{2}-?\d{7}$')
text = '0300-0000000'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^03\d{2}-?\d{7}$/';
$text = '0300-0000000';
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("^03\\d{2}-?\\d{7}$");
String text = "0300-0000000";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^03\d{2}-?\d{7}$`)
text := "0300-0000000"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^03\d{2}-?\d{7}$");
var text = "0300-0000000";
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"^03\d{2}-?\d{7}$").unwrap();
let text = "0300-0000000";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^03\d{2}-?\d{7}$/
text = '0300-0000000'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why does +92 300 0000000 not match?
- This pattern covers the local form that starts with 0. Use the international mobile pattern for +92 numbers, or convert them to the local form before checking.
- Can I tell the network from the prefix?
- Prefixes were originally assigned to networks, but mobile number portability lets people keep their number when they switch. The prefix is no longer a reliable guide to the current network.
- How do I store numbers in one format?
- Strip dashes and spaces, then convert a leading 0 to +92. Storing the international form makes numbers easy to use with SMS and calling services.