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
|
556 Discussions
|
Please Login in order to post a comment
I have used regex to solve this problem, and some python logics.
I have created 3 differents patterns (1 for each condition, except the last one which check at the same time if the UID is exactly 10 characters).
first condition pattern check if 2 uppercase (at least) are present or not.
Note : ?. is a lookaround operators and it means anything before an uppercase. So "....A....". This format must be repeated at least twice : {2,}
Exact same logics with digits 0-9
And the last pattern, I think, is logic to understand, we only want a-zA-Z0-9 characters exactly 10 times.
Then, we just have to check condition by condition.
Here my code, I am pretty sure there is a simple way or more concise way to solve this problem, but, it works and the logics sounds good :
import re
uid = list T = int(input()) pattern = r'^(?=(?:.[A-Z]){2,})(?=(?:.\d){3,})[A-Za-z\d]+$'
for _ in range(T): # print(T) id = input() if len(set(id))==10: if re.match(pattern, id): print("Valid") else: print("Invalid") else: print("Invalid")