문제

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