6

I am trying to catch all these numbers with a regex but I can't find a pattern.

Criteria for numbers:

  1. Numbers can start with "00"
  2. Numbers can start with "+"
  3. Numbers can contain spaces between them.

it may be that before or after the phone number you have text.

Regex:

\b[\+]?[(]?[0-9]{2,6}[)]?[-\s\.]?[-\s\/\.0-9]{3,15}\b

Sample phone Numbers:

00491234567890
+491234567890

0123-4567890

0123 4567 789
0123 456 7890
0123 45 67 789

+490123 4567 789
+490123 456 7890
+49 123 45 67 789

123 4567 789
123 456 7890
123 45 67 789


+49 1234567890
+491234567890

0049 1234567890
0049 1234 567 890

(0049)1234567890
(+49)1234567890

(0049) 1234567890
(+49) 1234567890



text text (0049) 1234567890 text text
text text (+49) 1234567890 text text

Thanks.

Ayman Arif
  • 1,456
  • 3
  • 16
  • 40
victorelec14
  • 178
  • 12
  • 1
    And how long can a phone number be? And how long can a chunk be? And can area codes contain parenthesis? You aren't being very specific with your criteria here. – Aplet123 Mar 31 '20 at 11:03
  • 11 digits (without +49 or 0049) , parenthesis maybe (+49) or (0049) – victorelec14 Mar 31 '20 at 11:10
  • Does it have to be +49 or can it +22 or any toerh code. Also, does it have to contain 11 digits or is it a range. If it is a range, how wide is that range? All your sample phone numbers seem to contain either 9 or 10 digits, not 11. – Aplet123 Mar 31 '20 at 11:12
  • yeah, can be another +49 or +22 or +34 , phone long is max 11 digits, +49 (+11 digits max) or 0049 (+11 digits max) – victorelec14 Mar 31 '20 at 11:20
  • May be you better use a library like libphonenumber to parse. https://www.npmjs.com/package/google-libphonenumber – Gihan Mar 31 '20 at 11:20
  • @toto why close ? – victorelec14 Mar 31 '20 at 11:36
  • Because the answer is in the duplicate. – Toto Mar 31 '20 at 11:45

2 Answers2

4

I've written this regex to find phone numbers:

(\(?(0{1,2}|\+)\d{1,2}\)?)?([ -]*\d+)+
Aplet123
  • 33,825
  • 1
  • 29
  • 55
0

I made a workaround on this. Let me know if this helps

^(\d|\+)[ \d-]+$
Ayman Arif
  • 1,456
  • 3
  • 16
  • 40