Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Indonesian phone number

Tags:

regex

I need a regular expression that can accept the following number format.

+62 361 222777
+62 813-444-5555
+62 812-3333-3333
+62 811 391 2103
+62 361-2277777
(0361) 227337
+62 8113912103
08134455555
0361-2277777 (*)
+62 812 3333 3333 (*)
+62 877 80803550 (*)
081339222111 (*)
081 339 222 111 (*)
+62 811338429196 (*)

Here is what I did so far:

\+62\s\d{3}[-\.\s]??\d{3}[-\.\s]??\d{3,4}|\(0\d{2,3}\)\s?\d+|0\d{2,3}\s?\d{6,7}|\+62\s?361\s?\d+|\+62\d+|\+62\s?(?:\d{3,}-)*\d{3,5}

The last six numbers (indicated by *) is not fully found by the regular expression I have above. Can someone help me? Thanks.

like image 626
Agus Sanjaya Avatar asked Jun 21 '17 08:06

Agus Sanjaya


1 Answers

My advice would be to try breaking problem into smaller ones.

You can solve like this:

(\+62 ((\d{3}([ -]\d{3,})([- ]\d{4,})?)|(\d+)))|(\(\d+\) \d+)|\d{3}( \d+)+|(\d+[ -]\d+)|\d+

You can see demo here. I've broke problem into smaller ones divided like this ()|()|..., so you can see what cases I was solving by deleting individual brackets.

Even easier way is:

\+?([ -]?\d+)+|\(\d+\)([ -]\d+)

You can see this version here.

like image 191
Aleksandar Makragić Avatar answered Oct 13 '22 05:10

Aleksandar Makragić