Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
^( (?:\(? (?:00|\+) (?:[1-4]\d\d|[1-9]\d?) \)?)? [\-\.\ \\\/]? )? ( (?:\(?\d{1,}\)? [\-\.\ \\\/]?){0,} ) (?: [\-\.\ \\\/]? (?: #| ext\.?| extension| x ) [\-\.\ \\\/]? (\d+) )?$
/
gmix

Description

based on (slightly) adopted regex from https://stackoverflow.com/a/20971688/1104502

It parses three groups:

  • international code
  • phone-number
  • extension

Helpful if you want to strip national-code for feeding a normalized "base phone number" into a search index. Additional replace all non-digits [^\d] in a 2nd phase too, to get a digit-only field, which you can search with wildcards or n-grams.

Submitted by spex66 - 7 years ago