Home > OS >  How can I check that there is both or no bracket at all in Regex
How can I check that there is both or no bracket at all in Regex

Time:10-05

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

A. (486)-619-9812
B. 632 831 1993
C. 232-618-93200
D. 621) 198 1082

Above Regex support to return two valid phone number, A, B and D. C is invalid because last line number support to be 4 but it is 5 digits. D is invalid because open bracket is missing.

Now my Regex return D as valid number. How can I check that there is with or without brackets in Regex.

CodePudding user response:

You can use

^(?:\([0-9]{3}\)|[0-9]{3})[-. ]?[0-9]{3}[-. ]?[0-9]{4}$

See the enter image description here

  • Related