0

My goal is to match domestic and international telephone numbers as much as possible, and my pattern works pretty except for a leading space on one test input. Also looking for any other general improvements for sample data that I have missed.

I am less interested in identifying if the telephone is in the correct format for the country, more that it is (ex: not strictly USA).

Pattern is as follows, and is testable with data content here: https://regex101.com/r/baJxxw/1

\+?1?[\ \-\.]?\(?\d{2,4}\)?([\ \-\.]?\d+)+(\ ?[xX]\d+)?

The input (greedy) that I am trying to match and is failing with intended result of 635-48018 but getting 635-48018 is:

(089) / 636-48018   Domestic  <-- fails (matches with leading space)

Full data sample is here (any actual numbers are accidental):

333-555-1075
333.555.1075
(333)555.1075
(333)555-1075
(333)555-1075 x4141
(333) 555-1075
(333) 555-1075 x4141
1 (333) 555-1075
1 (333) 555-1075 x4141
1-333-555-1075
1-333-555-1075 X4141
1.800.591.5911
1.800.591.5911 x41
1.800.591.5911X414166
1.800.591.5911 X4141
+1555789123
+4974339296
+591 74339296
+1 555 555 5554
+1 555 555 5554 x4141
+(591) 7433433
+(591) (4) 6434850  <--fail, but OK
0591 74339296
0001 5555555555
(0001) 5555555
59145678464
645-00-0000 <-- also matches social security number, but OK for my needs

555-3010            Local
(541) 555-3010      Domestic
+1-541-555-3010     International
1-541-555-3010      Dialed in the US
001-541-555-3010    Dialed from Germany
191 541 555 3010    Dialed from France

636-48018           Local (Germany telephone)
(089) / 636-48018   Domestic  <-- fails (matches with leading space)
089/636-48018       Domestic  <-- fails
+49-89-636-48018    International
19-49-89-636-48018  Dialed from France

Variant needed is PRCE (php) and ECMAScript.

Phil M
  • 432
  • 7
  • 16

0 Answers0