Search code examples
phpregexpreg-match

Simple phone number regex for php


I've been looking for a simple phone number regex that will match just this format: (XXX) XXX-XXXX <---with the parentheses and the space after them required

For the life of me, I can't figure out why this wont work (This was one that I tried making myself and have been tinkering with for hours):

^[\(0-9\){3} [0-9]{3}-[0-9]{4}$

I've looked everywhere online for a regex with that particular format matching to no avail. Please help?


Solution

  • The following regex works

    ^\(\d{3}\) \d{3}-\d{4}$
    
    ^ = start of line
    \( = matches parenthesis open
    \d = digit (0-9)
    \) = matches parenthesis close