Regular Expressions 101

Community Patterns

Lookahead phone test with formatting

1

Regular Expression
PCRE (PHP <7.3)

/
(?=^(?:\+|00)?(?:[\-\/\(\)]?\d){11}$)(?:00|\+)[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])[^\d]*([\d])$
/
gm

Description

Let's say a phone number is valid in case it has 11 digits only (13 in case of using 00 instead of +). Separators could be anywhere. Take the valid numbers and bring to common format. Original idea was deleting all non-digits but using look-ahead made impossible to use [^\d]+ for remove non-digits, it needs to capture each digits separately. Looking forward to prettier solution.

Submitted by zolo - 9 years ago