Python Forum
Regular Expressions with inputs - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Regular Expressions with inputs (/thread-5848.html)



Regular Expressions with inputs - MethodRunner147 - Oct-24-2017

Hi, I am trying to let a user input a username that is 8 characters long . Then if they enter it incorrectly, display an error message and ask them to enter a correct username. Every time I enter an 8 digit long username, it still says it is an invalid username. I have left the code below. Any help is appreciated.


import re
username = str(input("Please enter an 8 digit username: ")).lower()
if username ==("[a-z]{8}"):
    print("Valid username")
else:
    str(input("Please enter a valid username"))



RE: Regular Expressions with inputs - Mekire - Oct-24-2017

You haven't actually used the re module here.
If you wanted to use re you would need to change your conditional to something like:
if re.match("^[a-z]{8}$", username):
but... don't do that.

Just check the length:
if len(username) == 8 and username.isalpha():

No need for regular expressions here at all.