Contact
Phone Extension Regex
Office phone numbers often carry an extension written in several ways: ext. 204, Ext 204, extension 42 or simply x1234. This pattern finds those forms anywhere in a string, case-insensitively, and the word boundary in front prevents matches inside words like next or box. It is useful when parsing email signatures, cleaning CRM contact fields or splitting a pasted number into main line and extension. The honest limit is that a lone x followed by digits, such as a dimension written as x 40, is also treated as an extension.
Pattern (JavaScript)
/\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b/iMatches a word boundary, then (the text "ext", then a literal ".", optionally (zero or one time) OR the text "extension" OR the character "x"), then a space, optionally (zero or one time), then a digit, between 1 and 6 times, then a word boundary.
Examples
Matches
- ✓ 555-0100 ext. 204
- ✓ Call x1234
- ✓ extension 42
Does not match
- ✗ next 5 items
- ✗ box 12
- ✗ extra 12
Try it
3 matches (JavaScript engine)
555-0100 ext. 204 Call x1234 extension 42 next 5 items box 12 extra 12
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b/gi;
const text = "555-0100 ext. 204";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b', re.I)
text = '555-0100 ext. 204'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b/i';
$text = '555-0100 ext. 204';
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("\\b(?:ext\\.?|extension|x) ?[0-9]{1,6}\\b", Pattern.CASE_INSENSITIVE);
String text = "555-0100 ext. 204";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b`)
text := "555-0100 ext. 204"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b", RegexOptions.IgnoreCase);
var text = "555-0100 ext. 204";
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"(?i)\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b").unwrap();
let text = "555-0100 ext. 204";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\b(?:ext\.?|extension|x) ?[0-9]{1,6}\b/i
text = '555-0100 ext. 204'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I capture just the extension digits?
- Wrap [0-9]{1,6} in a capturing group, for example ([0-9]{1,6}), and read group 1 after a match. That gives you the digits without the ext label.
- Why is there a word boundary before the label?
- Without it, the x in words such as box or next would be treated as an extension marker. The boundary requires the label to start a word.
- Can I also accept a hash sign like #204?
- Add # as another alternative, but note that # does not create a word boundary, so place it outside the \b section or handle it as a separate pattern.