samedi 2 juillet 2016

Can re.fullmatch() eliminate the need for string anchors in regex

Consider the following regex, which checks for password strength. It has the start and end string anchors, to ensure it's matching the entire string.

pattern = re.compile(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}$')
    while True:
        user_pass = input('Enter a secure password: ')
        if re.fullmatch(pattern, user_pass):
            print('Successfully changed password')
            break
        else:
            print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
                  ' and special character.')

I noticed Python 3.5 has a re.fullmatch() which appears to do the same thing, but without the string anchors:

pattern = re.compile(r'(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}')
while True:
    user_pass = input('Enter a secure password: ')
    if re.fullmatch(pattern, user_pass):
        print('Successfully changed password')
        break
    else:
        print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
              ' and special character.')

Is this the intended purpose of fullmatch? Are there any situations where this could cause unintended issues?

Aucun commentaire:

Enregistrer un commentaire