Regular Expressions 101

Community Patterns

If a line seems valid phone number, change to common format

0

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 (or 13 in case we use 00 instead of + ). Take those lines to common format. original idea was to remove all non-digit characters from those lines with simple match like [^\d] but combined with look-ahead it just did not work, had to go through each digits to capture and put back. Looking forward to any prettier solution.

Submitted by zolo - 9 years ago