I need the REGEX for the following that should match both conditions
123-456-789 and 123456789
Need REGEX for the Following
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jean PaulRank #32 · 38.5K reputationPosted Jan 3, 2011, 10:23 PM EST
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
Jean PaulRank #32 · 38.5K reputationPosted Jan 3, 2011, 10:05 PM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 3, 2011, 10:09 PM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 3, 2011, 10:32 PM EST
(\d{3}-\d{3}-\d{3})|(\d{9})