v Match US Phone Numbers - Regex

Match US Phone Numbers

Based on: Regular Expressions Cookbook

Preliminaries

# Load regex package
import re

Create some text

# Create a variable containing a text string
text = 'My phone number is 415-333-3922. His phone number is 4239389283'

Apply regex

# Find any text that fits the regex
re.findall(r'\(?([2-9][0-8][0-9])\)?[-.●]?([2-9][0-9]{2})[-.●]?([0-9]{4})', text)
[('415', '333', '3922'), ('423', '938', '9283')]