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
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.
The plus ( + ) is a quantifier that matches one or more occurrences of the preceding element. The plus is similar to the asterisk ( * ) in that many occurrences are acceptable, but unlike the asterisk in that at least one occurrence is required.
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.
Formatting regular expressions The default setting is \1, which means that a zone name that matches the regular expression is replaced by the contents of the first variable created by the regular expression. In a regular expression, variable values are created by parenthetical statements.
no characters should repeat condition: regex to match a word with unique (non-repeating) characters
match exact number of characters condition: Regular expression to match exact number of characters?
the must contain condition: Regex must contain specific letters in any order
should contain only condition: it seems from the question you have already figured that out on your own.
The remaining job is to combine them all which you should do on your own if its part of an exercise given to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With