Meena
Meena

Reputation: 715

Regular Expression accepting only 9 digits, no special characters , may have leading zero's only up to 2

Hi i need a Regular Expression which should accept exactly 9 characters that to only Numbers from 0-9 ,but should not contain all Zeros ,should not contain special characters , may start with zero and leading zero's must be only up to 2. Regular expression should accept the below pattern

123456789
012345678
001234567

Right now i have ^[1-9][0-9]{8}$ this regular expression which is accepting 9 digits, no special characters, and should not start with zero.

Upvotes: 1

Views: 1158

Answers (2)

grail
grail

Reputation: 930

If RE doesn't have look-ahead then simply build your expression from scratch with all your criteria:

^(0[1-9]{8}|00[1-9]{7}|[1-9][0-9]{8})$

Again, depending on your engine you may need to escape pipes and some of the brackets :)

Upvotes: 0

Sebastian Proske
Sebastian Proske

Reputation: 8413

You can use a lookahead to check leading zero's must be only up to 2 (and should not contain all Zeros in the same step) like

^(?!0{3})[0-9]{9}$
  • (?!0{3}) discard any number starting with 3 zeros
  • [0-9]{9}match any other 9 digit number

Upvotes: 3

Related Questions