Validate UID in python using regex

39 Views Asked by At

I want to validate a UID based on the following conditions:

  1. at least two uppercase letters
  2. at least three digits
  3. must only contain alphanumeric characters (a-z,A-Z,0-9)
  4. no character should repeat
  5. must be exactly 10 characters

This is the correct regex: re.match(r'^(?=(?:.*[A-Z]){2,})(?=(?:.*\d){3,})(?!.*(.).*\1)[a-zA-Z0-9]{10}$', input())

The following two are my solutions:

#A  re.match(r'^(?=(.*[A-Z]){2,})(?=(.*\d){3,})(?!.*(.).*\1)[a-zA-Z0-9]{10}$',input())
#B  re.match(r'^(?=.*[A-Z]{2,})(?=.*\d{3,})(?!.*(.).*\1)[a-zA-Z0-9]{10}$',input())

A cannot match anything B can only match a few cases like:'AhBUw9r675','41726EHJIr','X6543g08QS', cantnot match cases like: 'yD09Ee83fJ','96R5ZDJg72','r57tH100Ej','h7AFN4y5dt'....etc

My question is why both of my solutions are wrong, and more specifically

for A: what are the differences between capturing and non-capturing group besides that one can return the match and be referenced while the other only search for match but return nothing

for B: when using lookahead or lookbehind assertion, when do I need to put the pattern in or not in () e.g. (?= pattern) (?= (pattern))

0

There are 0 best solutions below