1

Hi i need a Regular Expression which is accepting only Numbers from 0-9 ,but should not contain all Zeros ,should not contain special characters , should not start with zero and with a length of exact 9 digits.

I tried this one but it is accepting special characters at last. ^[1-9][0-9]{8}

Please help me out. Thanks in advance.

Meena
  • 685
  • 8
  • 31

3 Answers3

2

Have you tried

^[1-9]\d{8}$

Not sure why you are seeing special characters at the end unless it's after the 9 digits. the $ will solve that problem.

Additionally I prefer the use of character classes when applicable (\d for any digit). I think it makes, sometimes very complicated, Regex easier to read than groups.

micker
  • 878
  • 6
  • 13
2

Put a ending character at the end $

 ^[1-9][0-9]{8}$
C14L
  • 12,153
  • 4
  • 39
  • 52
  • i have one more requirement can you please provide a solution for my requirement, My requirement is A regular expression that may accept leading zero's only up to 2 Regular expression should accept 012312312 001231231 123123123 these 3 patterns. Please help me out.. – Meena Mar 09 '17 at 08:08
  • `^(?!000)[0-9]{9}$` That should probably do it. It uses a look-around at the first position to make sure there is no "000" there. More on [negative matching is in this answer](http://stackoverflow.com/a/5334825/5520354) – C14L Mar 11 '17 at 11:33
0

Try this change

^[1-9]\d{8}

I hope it works

Adolfo
  • 1,862
  • 13
  • 19