How to Extract Phone Numbers with Regex
Published · 5 min read
Phone numbers look simple until you try to find them in real text. One person writes 0300-0000000, another writes +92 300 0000000, a third writes (555) 555-0100, and a spreadsheet export has dropped the leading zero entirely. There is no single pattern that handles every number on earth correctly, and trying to write one usually produces something unreadable that still misses cases. This guide shows a more practical approach: decide what you need, choose a pattern that fits your data, and normalize the results in code.
Why phone numbers are harder than they look
Numbering plans differ by country in length, prefixes and grouping. Within one country, people format the same number with spaces, dashes, dots, brackets or nothing at all. Text also contains many other digit sequences that look similar: dates, order numbers, postal codes, amounts and IDs. A good phone pattern has to find the numbers you want while staying away from those neighbours.
Regex can only check that something has the shape of a phone number. It cannot tell you whether the number is assigned, active or belongs to the person who wrote it. Keep that limit in mind when deciding how strict to be.
Start by choosing extraction or validation
The same format leads to two different patterns depending on the job. For validation, such as checking a form field, you anchor the pattern with ^ and $ so the whole input must be a phone number. For extraction, you search inside longer text, so you use word boundaries \b instead and accept many matches. The validation vs extraction lesson covers the difference in detail.
You also need to decide how wide to cast the net. If all your data comes from one country, a precise local pattern gives the cleanest results. If it comes from many places, a looser pattern followed by cleanup in code is usually more reliable.
Patterns for specific local formats
Pakistani mobile numbers
Pakistani mobile numbers written in national format start with 03, followed by two more digits and then seven digits, often with a dash after the first four. This pattern accepts the dash as optional and uses boundaries so it does not match inside a longer number:
\b03\d{2}-?\d{7}\bIt matches 0300-0000000 and 03210000000 but not the too-short 0300-12345. The library has a fuller Pakistan mobile number pattern that also accepts the international +92 prefix.
North American style numbers
Numbers in the North American style have a three-digit area code, a three-digit exchange and a four-digit line number. The area code may appear in brackets. Because a bracket is not a word character, the pattern uses alternation: either a bracketed area code, or a plain one that starts at a word boundary.
(?:\(\d{3}\)\s?|\b\d{3}[-.\s])\d{3}[-.]\d{4}\bThis finds (555) 555-0100, 555.555.0199 and 555-555-0142. It is still a shape check only; it does not know which area codes exist.
Turning an extractor into a validator
Any of these patterns can check a single form field instead. Replace the outer word boundaries with anchors so nothing else is allowed before or after the number. For the Pakistani mobile format that gives the pattern below, which accepts 0300-0000000 on its own but rejects call 0300-0000000 because of the extra words. Trim surrounding spaces from the input before testing it, since users often paste them by accident.
^03\d{2}-?\d{7}$International numbers in E.164 format
E.164 is the international standard format used by many systems for storage: a plus sign, a country code that does not start with zero, and the rest of the number, with no spaces or punctuation and at most fifteen digits in total. That makes it the easiest format to match reliably:
\+[1-9]\d{6,14}\bThe lower bound of seven digits in total is a practical choice to avoid matching very short codes, not part of the standard. Adjust it if your data needs something different. If you control how numbers are stored, saving them in E.164 makes every later search much simpler. See the international phone number pattern for a version that also allows common separators.
A loose extractor for messy, mixed text
When numbers come from many countries in many styles, a precise pattern for each is impractical. A different strategy works better: match anything that looks like a run of digits with phone-style punctuation, then clean and check the candidates in code.
(?:\+|\()?\d[\d\s().-]{6,}\dThis matches an optional plus sign or opening bracket, a digit, at least six digits or separators, and a final digit. On the text Ring +92 300 0000000 or (555) 555-0100 on 12/03/2026. it returns the two phone numbers and skips the date, because a slash is not in the allowed set. It will, however, match other long digit runs, such as 100 200 300 400, so the cleanup step matters.
Normalizing the results in code
Normalization means turning every candidate into one consistent form so you can compare, deduplicate and store numbers. A simple approach is to keep the leading plus sign, strip every other non-digit, and then check the digit count.
const text = "Ring +92 300 0000000 or (555) 555-0100 on 12/03/2026.";
const candidates = text.match(/(?:\+|\()?\d[\d\s().-]{6,}\d/g) ?? [];
const numbers = candidates
.map((c) => (c.startsWith("+") ? "+" : "") + c.replace(/\D/g, ""))
.filter((n) => {
const digits = n.replace("+", "").length;
return digits >= 7 && digits <= 15;
});
console.log(numbers); // ["+923000000000", "5555550100"]import re
text = "Ring +92 300 0000000 or (555) 555-0100 on 12/03/2026."
candidates = re.findall(r"(?:\+|\()?\d[\d\s().-]{6,}\d", text)
numbers = []
for c in candidates:
digits = re.sub(r"\D", "", c)
if 7 <= len(digits) <= 15:
numbers.append(("+" if c.startswith("+") else "") + digits)
print(numbers) # ['+923000000000', '5555550100']If you know which country the national numbers belong to, you can go one step further and convert them to E.164 by replacing the leading zero with the country code. For anything beyond that, such as checking whether a number is a mobile or landline, a dedicated phone number library maintained against real numbering plans is more dependable than any regex.
0300-0000000 or 555-0100 when testing, and handle real contact lists according to the privacy rules that apply to you. RegexLab runs entirely in your browser, so text you paste into it is not uploaded anywhere.Common pitfalls
- Missing boundaries: without
\b, a seven-digit pattern will match the middle of a longer ID or card number. - Matching dates and amounts: exclude
/and,from the separator set unless your numbers really use them. - Separators crossing lines:
\salso matches newlines, so a loose pattern can join numbers from two lines. Use a literal space instead if that is a problem. - Over-strict patterns: requiring one exact format silently drops valid numbers written slightly differently. Check what your data really contains first.
- Assuming a match is real: a correctly shaped number may still be unassigned or mistyped.
- Ignoring extensions: text such as
ext. 12after a number is not captured by these patterns. Add an optional group for it if your data includes extensions.
Next steps
Collect a realistic sample of your own text, including lines that contain dates, prices and IDs, and test each candidate pattern against it. Note which numbers it catches and which it misses, then pick the strictest pattern that still finds everything you need. The pattern generator can build a starting pattern from a description of your format. For more on extracting values of any shape, read how to extract anything from text.
Frequently asked questions
- Is there one regex that matches every phone number in the world?
- Not in a useful way. Formats vary too much between countries. A loose pattern plus normalization in code, or a dedicated phone number library, works better than one giant regex.
- What is the E.164 format?
- E.164 is the international format of a plus sign, a country code and the national number, with no spaces or punctuation and at most fifteen digits. It is a good format for storing numbers.
- How do I stop my phone pattern from matching dates?
- Leave `/` out of the allowed separators, add word boundaries, and require a minimum number of digits so short date parts cannot match on their own.
- Can regex check whether a phone number is real?
- No. Regex only checks the shape of the text. Whether a number is assigned or reachable can only be confirmed by other means.
Related posts
- How to Extract Anything from Text with RegexA repeatable regex method for pulling codes, emails, dates and amounts out of messy text, with tested patterns and JavaScript and Python code.
- How to Clean Messy Data with RegexPractical regex find-and-replace recipes for messy data: whitespace, duplicate words and lines, dates, phone numbers, leftover HTML and CSV fields.
- How to Extract Data from a WhatsApp Chat ExportParse a WhatsApp chat export with regex: match Android and iOS lines, handle multi-line messages, and pull out senders, dates and links privately.