US/Canada Phone Number (NANP) Regex for Python
/^(?:\+1[\s.-]?)?(?:\(([2-9][0-9]{2})\)|([2-9][0-9]{2}))[\s.-]?([2-9][0-9]{2})[\s.-]?([0-9]{4})$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching us/canada phone number (nanp), ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# US/Canada Phone Number (NANP)
# ReDoS-safe | RegexVault — Localization > Phone Numbers
import re
uscanada_phone_number_nanp_pattern = re.compile(r'^(?:\+1[\s.-]?)?(?:\(([2-9][0-9]{2})\)|([2-9][0-9]{2}))[\s.-]?([2-9][0-9]{2})[\s.-]?([0-9]{4})$')
def validate_uscanada_phone_number_nanp(value: str) -> bool:
return bool(uscanada_phone_number_nanp_pattern.fullmatch(value))
# Example
print(validate_uscanada_phone_number_nanp("+1 212-555-0100")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
+1 212-555-0100 | 112-555-0100 |
212-555-0100 | 212-055-0100 |
(212) 555-0100 | 212-555-010 |
212.555.0100 | +44 212-555-0100 |
2125550100 | 800-1234567 |
When to use this pattern
This pattern is drawn from the Localization > Phone Numbers category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Canada shares the +1 country code with the US. Area codes do not reliably indicate country — some Canadian area codes (e.g., 416=Toronto) look identical to US codes in format.
Technical Notes
NANP area codes and exchange codes cannot start with 0 or 1. Capture groups: 1/2=area code, 3=exchange, 4=subscriber. 555-0100 through 555-0199 are reserved for fiction. Toll-free: 800, 888, 877, 866, 855, 844, 833.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern