Search code examples
c#regexdigit

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


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
    }

Solution

  • 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.