Home > database >  Regex for the number that should not start with some specific two digit numbers
Regex for the number that should not start with some specific two digit numbers

Time:09-20

I am trying to build a regex for following rules

  1. No characters – only numbers are allowed to enter
  2. Numbers must contain not less than 8 digits
  3. Numbers and combinations, starting as follows should not be allowed:
    • [3.1] - Any number starting with 0 (zero) and with 1 (one);
    • [3.2] - Numbers starting with 20, 21, 22, 23, 24, 25, 26, and 27.

I am able to achieve regex for points 1, 2 and 3.1 like this

^[2-9]{1}[0-9]{7,}$

But I am not able to find solution for point 3.2

This is one of the options I started with, this regex matches the string that must start with 20, 21, 22, 23, 24, 25, 26, or 27.

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

I just have to find the negation of this first condition.

Please help!

CodePudding user response:

Rather than trying to negate the 20-27 condition, just match numbers that start with 28 or 29 instead:

^(?:2[89]|[3-9]\d)\d{6,}$

Demo on regex101

  • Related