Regular expression in C# - Regex accepting numbers with DDD and accepting numbers without DDD

Asked

Viewed 64 times

0

Guys, how do I create a regular expression, where she should accept numbers with DDD, and also accept numbers without DDD?

1 answer

3

Regex reg = new Regex(@"(\([0-9]{2}\)|)[0-9]{4,5}-[0-9]{4}");
reg.isMatch("(12)12345-1234"); //retorna true
reg.isMatch("(12)1234-1234"); //retorna true
reg.isMatch("12345-1234"); //retorna true
reg.isMatch("1234-1234"); //retorna true
reg.isMatch("123-1234"); //e qualquer outra combinação retorna false
  • Thank you very much Leandro... My mistake was that I was not parenting this part : @"((([0-9]{2})|). I thank you for your help.

  • @Felipemoreira, if the answer solved your problem, you can mark as Answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.