I am doing form validation for a password using Python and Flask. The password needs to contain at least one uppercase letter and at least one number.
My current failed attempt…
re.compile(r'^[A-Z\d]$')
flaskpythonregex
I am doing form validation for a password using Python and Flask. The password needs to contain at least one uppercase letter and at least one number.
My current failed attempt…
re.compile(r'^[A-Z\d]$')
Best Answer
We can use the pattern
'\d.*[A-Z]|[A-Z].*\d'
to search for entries that have at least one capital letter and one number. Logically speaking there are only two ways that a capital letter and a number can appear in a string. Either the letter comes first and the number after or the number first and the letter after.The pipe | indicates 'OR', so we will look at each side separately.
\d.*[A-Z]
matches a number that is followed by a capital letter,[A-Z].*\d
matches any capital letter that is followed by a number.