2

Acceptable input : any 9 digit number

Not acceptable : 123456789 and 987654321

I am using [0-9]{9} but I want extra condition as well

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91

2 Answers2

1

I would suggest that, as you only have two exceptions, you write the regex to confirm it's a 9-digit numeric number, and then special case the two edge cases you mention. Not as technically challenging, but much easier to read the code.

Black Light
  • 2,358
  • 5
  • 27
  • 49
1

Try:

^(?!123456789|987654321)\d{9}$

Regex demo.


^ - beginning of string

(?!123456789|987654321) - don't continue matching if 123456789 or 987654321 is found

\d{9} - match 9 digits

$ - end of string

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91