tags:

views:

1758

answers:

5

I'm looking for a regular expression that will match text given the following requirements:

  • contains only 10 digits (only numbers)
  • starts with 9.

These examples should match:

  • 9999999999
  • 9876543210

These examples should not match:

  • 999999999
  • 1234567890
  • 8912456789
  • qwe3456&ert

It is basically for Indian mobile numbers.

Please provide examples, I have already searched Google and those answers provide over-validation.

+10  A: 

Try something like this:

^9\d{9}$
Andrew Hare
thanx man!!it worked..
knowledgehunter
Then the next task you have, @knowledgehunter, is to click on that big honkin' green tick mark next to this answer.
paxdiablo
+1  A: 

9{1}\d{9} or 9{1}[0-9]{9}

SP
Isn't the {1} in 9{1} kind of redundant?
1800 INFORMATION
Also, this matches twenty 9's in a row. You need anchors.
strager
I think it would match the first ten and the next ten in separate matches.
SP
+4  A: 

I always prefer the REs that can be used on any engine (so no fancy "\d" things):

^9[0-9]{9}$

or, in a crunch,

^9[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$

if the RE engine doesn't even have "{}".

Yes, I know the C# engine can do both "\d" and "{}" but that's not always the case.

paxdiablo
This is very interesting - I wasn't aware of any regular expression implementations that do not support `\d` or `{n}`.
Andrew Hare
Some of the "lesser" RE tools (sed/grep/etc) on older UNIXes don't have them. Especially on systems that have different binaries in single-user mode (statically linked so as to have no dependencies).
paxdiablo
Very cool - good to know!
Andrew Hare
+1  A: 

^9[0-9]{9}$

neverland
A: 

Try this it is tested

^([9]{1})([0-9]{9})$