I need the REGEX for the following that should match both conditions
123-456-789 and 123456789
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jean PaulPosted Jan 4, 2011, 3:23 AM
You can use the following code - it solves your problem:
static void Main(string[] args)
{
bool result = IsValid("123-456-789");
}
private static bool IsValid(string input)
{
bool result1 = new Regex("^([0-9]{3}[-][0-9]{3}[-][0-9]{3})$").IsMatch(input);
bool result2 = new Regex("^([0-9]{9})$").IsMatch(input);
return result1 || result2;
}
// Here separate comaprison is done as I am not a regex expert :)
Regards,
Jean Paul
Please mark as answer if correct
Dorababu MekaPosted Jan 4, 2011, 3:32 AM
(\d{3}-\d{3}-\d{3})|(\d{9})
Dorababu MekaPosted Jan 4, 2011, 3:09 AM
Jean PaulPosted Jan 4, 2011, 3:05 AM