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

0

People, how do I create a regular expression, in which it should accept numbers with DDD, and should I also accept non-DDD numbers?

    
asked by anonymous 12.05.2016 / 22:06

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
    
12.05.2016 / 22:23