We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Regex and Parsing
- Validating UID
- Discussions
Validating UID
Validating UID
Sort by
recency
|
529 Discussions
|
Please Login in order to post a comment
''' (?=(?:.\d.){3,}) => This is a lookahead to check that the string contains at least 3 digits (0-9). (?=(?:.[A-Z].){2,}) => This is a lookahead to check that the string contains at least 2 Uppercase letters (A-Z). (?!.(.).\1) => ensure that no character repeats. '^', '$' [a-zA-Z0-9]{10} => This part matches exactly 10 alphanumeric characters. ''' import re
for _ in range(int(input())): print("Valid" if re.match(r"^(?=(?:.\d.){3,})(?=(?:.[A-Z].){2,})(?!.(.).\1)[a-zA-Z0-9]{10}$", input()) else "Invalid")
i tried to write the code in a clearer way, using re.VERBOSE, however i am unable to complete the excercise with this. in fact, test 3 fails expecting, for example, string "944A4NKtE2" to be valid (even though the character '4' is repeating). How so? is someone able to help me understand this?
import re
def is_valid_uid(uid): if len(uid) != 10 or not uid.isalnum(): return False if len(re.findall(r'[A-Z]', uid)) < 2: return False if len(re.findall(r'\d', uid)) < 3: return False if len(set(uid)) != len(uid): return False return True
if name == 'main': n = int(input()) for _ in range(n): uid = input().strip() if is_valid_uid(uid): print("Valid") else: print("Invalid")