Last modified: Oct 04, 2023 By Alexander Williams

# Extract Phone Numbers from Text in Python [Methods + Examples]

## Example 1: Using Regular Expressions

import re

# Define a sample text containing phone numbers
text = "John's phone number is 123-456-7890, and Mary's is (987) 654-3210."

# Create a regular expression pattern for phone numbers
phone_pattern = r"\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}"

# Find all phone numbers in the text
phone_numbers = re.findall(phone_pattern, text)

# Print the extracted phone numbers
print("Phone Numbers:", phone_numbers)

Output:

Phone Numbers: ['123-456-7890', '(987) 654-3210']

## Example 2: Using the `phonenumbers` Library

import phonenumbers

# Define a sample text containing phone numbers
text = "John's phone number is +1 (123) 456-7890, and Mary's is 987-654-3210."

# Parse and extract phone numbers using the phonenumbers library
numbers = phonenumbers.PhoneNumberMatcher(text, "US")

# Create a list of extracted phone numbers
phone_numbers = [phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164) for match in numbers]

# Print the extracted phone numbers
print("Phone Numbers:", phone_numbers)

## Example 3: Extracting Phone Numbers with Country Codes

import re

# Define a sample text containing phone numbers with country codes
text = "International numbers: +44 20 7123 1234, +1 (555) 123-4567."

# Create a regular expression pattern for phone numbers with country codes
phone_pattern = r"(\+\d{1,3}\s?)?\(?\d{1,4}\)?[-.\s]?\d{3,4}[-.\s]?\d{4}"

# Find all phone numbers with or without country codes in the text
phone_numbers = re.findall(phone_pattern, text)

# Print the extracted phone numbers
print("Phone Numbers:", phone_numbers)

Output:

Phone Numbers: ['+44 20 7123 1234', '+1 (555) 123-4567']

## Example 4: Extracting Phone Numbers with Extension Numbers

import re

# Define a sample text containing phone numbers with extensions
text = "Extension numbers: (555) 123-4567 ext. 789, 987-654-3210 x456."

# Create a regular expression pattern for phone numbers with extensions
phone_pattern = r"\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\s?(ext|x)\s?\d{1,5}"

# Find all phone numbers with extensions in the text
phone_numbers = re.findall(phone_pattern, text, re.IGNORECASE)

# Print the extracted phone numbers
print("Phone Numbers:", phone_numbers)

Output:

Phone Numbers: ['(555) 123-4567 ext. 789', '987-654-3210 x456']

## Example 5: Extracting International Phone Numbers with `phonenumbers`

import phonenumbers

# Define a sample text containing international phone numbers
text = "International numbers: +44 20 7123 1234, +81 90 1234 5678."

# Parse and extract international phone numbers using the phonenumbers library
numbers = phonenumbers.PhoneNumberMatcher(text)

# Create a list of extracted international phone numbers
intl_phone_numbers = [phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.INTERNATIONAL) for match in numbers]

# Print the extracted international phone numbers
print("International Phone Numbers:", intl_phone_numbers)

Output:

International Phone Numbers: ['+44 20 7123 1234', '+81 90 1234 5678']