Pakistan Special
Pakistan Mobile Number With Spaces Regex
Business cards, websites and printed flyers often show mobile numbers with spaces for readability, such as 0300 0000000 or +92 300 0000000. This pattern accepts a leading 0 or +92 with an optional space, then the 3 and two prefix digits, a single space, and the seven subscriber digits. It rejects dashes, unspaced runs and double spaces, so it is best used where you want to enforce a single readable display style, for example when checking formatted numbers in a content file. For free-form user input, remove all spaces and check the compact forms instead, since people type spacing in many different ways.
Pattern (JavaScript)
/^(?:\+92\s?|0)3\d{2}\s\d{7}$/Starts at the start of the line/string, then (the text "+92", then a whitespace character (space, tab, newline), optionally (zero or one time) OR the character "0"), then the character "3", then exactly 2 digits, then a whitespace character (space, tab, newline), then exactly 7 digits, then the end of the line/string.
Examples
Matches
- ✓ 0300 0000000
- ✓ +92 300 0000000
- ✓ +92300 1234567
Does not match
- ✗ 03000000000
- ✗ 0300-0000000
- ✗ 0300 0000000
Try it
0 matches (JavaScript engine)
0300 0000000
+92 300 0000000
+92300 1234567
03000000000
0300-0000000
0300 0000000Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:\+92\s?|0)3\d{2}\s\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'^(?:\+92\s?|0)3\d{2}\s\d{7}$')
text = '0300 0000000'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:\+92\s?|0)3\d{2}\s\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("^(?:\\+92\\s?|0)3\\d{2}\\s\\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(`^(?:\+92\s?|0)3\d{2}\s\d{7}$`)
text := "0300 0000000"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:\+92\s?|0)3\d{2}\s\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"^(?:\+92\s?|0)3\d{2}\s\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 = /^(?:\+92\s?|0)3\d{2}\s\d{7}$/
text = '0300 0000000'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is a double space rejected?
- The pattern allows exactly one whitespace character between the prefix and the subscriber digits. That keeps the display format consistent.
- Should users be forced to type spaces?
- Usually not. For input fields, strip spaces and dashes first and check the compact form; use this pattern for formatted display text.
- Does it accept 0092 as a prefix?
- No. Only 0 and +92 are allowed. Add 0092 as another alternative in the first group if your data uses it.