Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Pattern for Philippine Phone Number

Tags:

regex

There are two types of phone number in Philippines.

First Type = it start with 09 followed by 9 numbers. I already did this. This is my pattern for this.

^(09)\\d{9}

and it is working.

Second Type = it start with +639 followed by 9 numbers. Example is +6391571825. I can't identify the pattern for this because it has special character. What is the pattern for this?

like image 697
user3802633 Avatar asked Jul 02 '15 03:07

user3802633


2 Answers

Just escape the special character and place it in alternation as

^(09|\+639)\d{9}$
  • (09|\+639) Alternation matches either 09 or +639

  • $ Anchors the regex at the end of the string. This ensures that no more digits can appear after the 9 characters.

Regex Demo

like image 112
nu11p01n73R Avatar answered Nov 15 '22 11:11

nu11p01n73R


I use this regex: ((^(\+)(\d){12}$)|(^\d{11}$))

It matches either of the following formats 1. +639191234567, or 2. 09191234567

Explanation

There are 2 chunks combined in 1 expression. Note that you can hardcode "63" in the expression and change the first chunk like so (^(\+63)(\d){10}$)

  • (^(\+)(\d){12}$) matches the mobile number with area code i.e. +63927...
  • ^(\+) - starts with a +
  • (\d){12}$ - ends with any digit of length 12
  • (^\d{11}$)) matches the number without the area code i.e. 0917...
  • \+ - '+' literal
  • \d - matches a digit from 0 - 9
  • {12} - matches the length 12
  • ^ - starts with
  • $ - ends with

I test my regex here: regex101.com

Happy coding! :)

like image 32
Alexa Avatar answered Nov 15 '22 10:11

Alexa