Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading