Regex: match phone numbers starting with NNN and having 10 numbers

I need to match phone numbers that are:

  • start with 010 or 012 or 016 or 019

  • Consists of 10 numbers exactly

Can you help me on how to match numbers using PHP and regex?

+2


a source to share


3 answers


return preg_match('/^01[0269]\\d{7}$/', $theStringToTest);

      



This will match 0, 1, one of (0, 2, 6, 9) and then any 7 numbers (3 + 7 == 10). ^

means the beginning of a line, and $

means the end of a line.

+4


a source


I guess I would use ^01[0269][0-9]{7}$



0


a source


Use this regex

/\b(010|012|016|019)[0-9]{7}\b/g

      

0


a source







All Articles