سؤال

im looking for a regex which can be used to detect exactly "071-xxxxxxx" where x is a digit. for an example 0712-954900 matches the scenario.can anyone help me.I tried following code.But it is not working.

    string phoneNumber = "0712954900";
    Regex regEx = new Regex(@"\b0\7\1\-\d\d\d\d\d\d\d");
    if (regEx.IsMatch(phoneNumber))
    {
          //do something
    }
هل كانت مفيدة؟

المحلول

Regular expression to detect exactly “071-XXXXXXX” where X is a digit

Here your are:

Regex regEx = new Regex(@"^071-[0-9]{7}$");

But it will not execute // do something for your sample code, because it's missing the hyphen.

نصائح أخرى

try this

string phoneNumber = "071-2954900";
Regex regEx = new Regex(@"071[-][\d]{7}");
if (regEx.IsMatch(phoneNumber))
{
      //do something
}

check here

^071-.{7,7}$ This regex will match 071-&7digit number for ex:071-9864527

This will match the phone number even with or without the "-"

 Regex regEx = new Regex(@"^\d{3}\-?\d{7}$");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top