I have just started out on the regex part of python and I thought I understood this concept but when I started out the programming I wasn't able to get it. The problem statement which was given is to design a regex which
- It must contain at least 2 uppercase English alphabet characters
- It must contain at least 3 digits (0-9)
- it should only contain alphanumeric characters
- No characters should repeat
- There must be exactly 10 characters
The code which I wrote is
import re
n=int(input())
patt=r'^(?=.*[A-Z]).{2,}(?=.*[0-9]).{3,}(?=.*[\w]?){10}$'
for x in range(n):
match=re.findall(patt,str(input()))
#print(match)
if match:
print("Valid")
else:
print("Invalid")
I first started out with the 1st part i.e should contain "It must contain at least 2 uppercase English alphabet characters" for which I wrote (?=.*[A-Z]).{2,}
as it will search for more than two characters and will use lookahead assertions
For the second part I applied the same and for the third part i.e it should only contain alphanumeric characters I applied (?=.*[\w]?)
these three seems to work but when the fourth and fifth condition comes i.e No characters should repeat and There must be exactly 10 characters I tried to use {10}
at last but it didn't work and the whole thing seems to be broken now. Can anyone guide me how to use regex and what exactly is a positive lookahead.