Python Forum
Regular Expressions with inputs
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular Expressions with inputs
#1
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"))
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recursive regular expressions in Python risu252 2 1,207 Jul-25-2023, 12:59 PM
Last Post: risu252
Sad Regular Expressions - so close yet so far bigpapa 5 950 May-03-2023, 08:18 AM
Last Post: bowlofred
  Having trouble with regular expressions mikla 3 2,581 Mar-16-2021, 03:44 PM
Last Post: bowlofred
  Statements and Expressions Julie 1 1,627 Feb-26-2021, 05:19 PM
Last Post: nilamo
  Regular Expressions pprod 4 3,067 Nov-13-2020, 07:45 AM
Last Post: pprod
  Format phonenumbers - regular expressions Viking 2 1,890 May-11-2020, 07:27 PM
Last Post: Viking
  regular expressions in openpyxl. format picnic 0 2,473 Mar-28-2020, 09:47 PM
Last Post: picnic
  Unexpected (?) result with regular expressions guraknugen 2 2,205 Jan-18-2020, 02:33 PM
Last Post: guraknugen
  Strange output with regular expressions newbieAuggie2019 1 1,925 Nov-04-2019, 07:06 PM
Last Post: newbieAuggie2019
  Regular Expressions amitalable 4 2,762 Mar-14-2019, 04:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020