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 alphanumeric characters but all of which are not zero

Conditions:-

  1. Total length = 12
  2. First letter = A
  3. Second letter = B
  4. Rest of the 10 characters = Numbers or alphabets
  5. Rest of the 10 characters cannot be equal to 0000000000

Valid:-

  1. AB1234567890
  2. ABABABABABAB
  3. AB1234HIJ001

Invalid:-

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

  1. AB0000000000
  2. AB0
  3. AA1234567890

I have come up with this regex: '^[A-A][B-B][A-Z0-9]{10}$'. It prevents Invalid #2 and #3. But I’m having a hard time with Invalid #1. I know that to prevent all characters from being 0, I need to use '^0+$'. But how do I combine these two expressions?

>Solution :

You can add a negative lookahead to accomplish this: (?!0{10}).

^[A-A][B-B](?!0{10})[A-Z0-9]{10}$
           ^^^^^^^^^

regex101 demo

By the way, you don’t need [A-A][B-B]. Just AB does exactly the same.

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