Home > Net >  Regex for alphanumeric characters but all of which are not zero
Regex for alphanumeric characters but all of which are not zero

Time:03-31

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:-

  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?

CodePudding user response:

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.

  • Related