user866364
user866364

Reputation:

How to check if a password is valid and matches a regular expression in Python

I'm writing a function that sometimes will receive a massive calls (near 2k/minute) and it should check if the password is valid.

My requirements are simple:

For now, I have code my function as:

import re

def check(p):

    return re.match(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{5}$", p)

I have some questions:

Upvotes: 1

Views: 85

Answers (1)

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39800

The following should do the trick (note the {5,}):

re.match(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{5,}$", p)

In other words, {5,} means 5 or more as opposed to {5} that means exactly 5.

Upvotes: 1

Related Questions