tags:

views:

69

answers:

3

Sir, I want regular expression for indian mobile numbers which consists of 10 digits. The number's which should match start with 9 or 8 or 7. i.e. 9882223456 , 8976785768 , 7986576783

it should not match the numbers starting with 1 to 6 or 0.

Thank you in advance.

+2  A: 

Here you go :)

^[7-9][0-9]{9}$
lasseespeholt
+6  A: 

^[789]\d{9}$ should do the trick.

^     #Match the beginning of the string
[789] #Match a 7, 8 or 9
\d    #Match a digit (0-9 and anything else that is a "digit" in the regex engine)
{9}   #Repeat the previous "\d" 9 times (9 digits)
$     #Match the end of the string
eldarerathis
beautiful explanation (+1)
seanizer
@Martin: Good point. I'll clarify that in my explanation.
eldarerathis
@eldarerathis: I have deleted my previous comment because I found it to be incorrect. The `\d` will apparently match any numeral known by the regex engine no matter what the current culture is. That is, it will match both latin, arabic and punjabi numerals etc.
Martin Liversage
A: 

To reiterate the other answers with some additional info about what is a digit:

new Regex("^[7-9][0-9]{9}$")

Will match phone numbers written using roman numerals.

new Regex(@"^[7-9]\d{9}$", RegexOptions.ECMAScript)

Will match the same as the previous regex.

new Regex(@"^[7-9]\d{9}$")

Will match phone numbers written using any numerals for the last 9 digits.

The difference is that the first two patterns will only match phone numbers like 9123456789 while the third pattern also will match phone numbers like 9੧੨੩੪੫੬੭੮੯.

If say you want to get digit 7-9 for punjabi (India) (to be able to match ੯੧੨੩੪੫੬੭੮੯) you can do it like this:

CultureInfo.GetCultureInfo("pa-IN").NumberFormat.NativeDigits.Skip(7).Take(3)

You can then join them together to form a "culture aware" regular expression for the digits 7-9.

Martin Liversage