Monday, April 22, 2019

Regex Phone number with dashes




I'm trying to get a regex that will validate the following formats:



234-567-8901
1-234-567-8901


I have tried various examples, but not sure how to get the one that will take into account the possible 1- at the begging and still inforce the 234-567-8901.



Rules are, any digit, 2 or 3 dashes, if 3 dashes then 11 numberes, if 2 dashes then 10 numbers.




^[0-9-]*$
^[\d-]+$
^[\d-]+$

Answer



You can use:



^(1-)?\d{3}-\d{3}-\d{4}$

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...