Regex for Phone with extension
Phone number followed by ext./x and digits.
The Phone with extension regex is ^.+(\s+(ext\.?|x|extension)\s*\d+)$ — copy it below, or open it in the explainer for a token-by-token breakdown.
The pattern
^.+(\s+(ext\.?|x|extension)\s*\d+)$
What it matches
555-1234 ext. 567+1-555-1234 x42(555) 123-4567 extension 100
What it doesn't match
555-1234555-1234 ext
Notes & gotchas
Matches an extension at the end of a phone number. Group 1 captures the extension portion. Common in office/business numbers.
Code in your language
Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.
Open in explainer →Token-by-token breakdown
Every part of the pattern, left to right:
| Token | Meaning |
|---|---|
^ | start of string (or line in multiline mode) |
.+ | one or more of any character |
( | start of a capturing group |
\s+ | one or more: whitespace |
( | start of a capturing group |
ext | literal text “ext” |
\.? | optional (zero or one): literal “.” |
| | OR — try the alternative |
x | literal text “x” |
| | OR — try the alternative |
extension | literal text “extension” |
) | end of group |
\s* | zero or more: whitespace |
\d+ | one or more: digit (0–9) |
) | end of group |
$ | end of string (or line in multiline mode) |
About this pattern
Identifying and validating user identity (names, contact info, IDs) is one of the most common reasons developers reach for regex. The pattern below handles the format check; for full validation always confirm against a source of truth (database, API, or document).
Quick usage in different languages
This exact pattern — with the correct escaping and idioms for each language:
- JavaScript:
/^.+(\s+(ext\.?|x|extension)\s*\d+)$/.test(value) - Python:
re.match(r"^.+(\s+(ext\.?|x|extension)\s*\d+)$", value) - Java:
Pattern.compile("^.+(\\s+(ext\\.?|x|extension)\\s*\\d+)$").matcher(value).matches() - C# / .NET:
Regex.IsMatch(value, @"^.+(\s+(ext\.?|x|extension)\s*\d+)$") - Go:
regexp.MustCompile(`^.+(\s+(ext\.?|x|extension)\s*\d+)$`).MatchString(value) - Ruby:
/^.+(\s+(ext\.?|x|extension)\s*\d+)$/.match?(value) - PHP:
preg_match('~^.+(\s+(ext\.?|x|extension)\s*\d+)$~', $value)
The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.
Common pitfalls
- Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
- Greedy “.*”. A greedy .* / .+ can match more than intended. Use a lazy version (.*?) or a negated class ([^…]) to stop at the right place.
- Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
- Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the phone with extension against a source of truth (database, API, or checksum) where it matters.
Related patterns
More patterns in the Identity & contact category:
See also
Browse all 300 patterns in the library, or open this regex in the interactive explainer to see a token-by-token breakdown, test against custom input, and generate code in seven languages.