1

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
    }
Daryl
  • 339
  • 1
  • 5
  • 17

4 Answers4

3

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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Thank you MarcinJuraszek.. I tried your answer and it is working for me.Now i can customize it according to my requirement.Thank u again :) – Daryl Jan 21 '14 at 06:50
2

try this

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

check here

user3064914
  • 921
  • 1
  • 7
  • 18
1

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

Shreyas Achar
  • 1,407
  • 5
  • 36
  • 63
1

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

 Regex regEx = new Regex(@"^\d{3}\-?\d{7}$");
PAVITRA
  • 761
  • 2
  • 12
  • 24