Regular Expressions 101

Community Patterns

finding phone numbers

1

Regular Expression
PCRE (PHP <7.3)

/
\D?[0-9]{3}\D?\s?[0-9]{3}\D?\s?[0-9]{4}
/
gmi

Description

we know phone numbers are [one group of 3 numbers] [another group of 3 numbers] [a group of 4 numbers]

we use D? as a way to find nondigits like the parenthesis

This should also work: (?\d{3})?\s?-?.?\d{3}\

Submitted by anonymous - 8 years ago