Regular Expressions 101

Community Patterns

Capture digits and extension of North American phone number (North American Numbering Plan)

1

Regular Expression
PCRE (PHP <7.3)

/
(?:[\+1\(-. ]{1,})?(?<ac>[2-9]{1}[0-9]{2})(?:[\(-. ]{1,})?(?<coc>[2-9]{1}[0-9]{2})(?:[\(-. ]{1,})?(?<sn>[0-9]{4})(?:\d{0,})?(?:\s*(?:#|x\.?|extension|ext\.?)\s*)?(?<ext>[a-zA-Z0-9. ]{0,})$
/
gm

Description

  1. strips Country Code 1 &/or token
  2. ${ac} or ${1} captures three-digit NXX Numbering Plan Area Code/National Destination Code
  3. strips separators
  4. ${coc} or ${2} captures three-digit NXX Central Office Code
  5. strips separators
  6. ${sn} or ${3} captures four-digit XXXX Station Number/Subscriber Number
  7. strips extra digits in vanity phone numbers
  8. strips extension delimiter
  9. ${ext} or ${4} captures n-digit extension
Submitted by anonymous - 4 years ago