I am trying to figure out how to setup a regex expression on doing the following requirements.
Basically a good example is
Acceptable Usernames
Unacceptable Usernames
If any help would appreciate, this is what I had but its not working for me as what I want it to be.
^(?=[A-Za-z0-9])(?!.*[_-]{2})[A-Za-z0-9_-]+$
valid_username(username) # if it isn't valid we should tell them why if not(result): print (reason) # otherwise the username is good - ask them for a password # get a password from the user password = input("Password: ") # determine if the password is valid pwresult, pwreason = uservalidation.
[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times.
If I understand your requirement correctly, you just need to validate that the username is correct? If so, I'd use this regex:
^[A-Za-z]{2,}[_-]?[A-Za-z0-9]{2,}$
You did not say how many characters would be required after the dash or underscore; my example requires at least 2 more after. It can be altered as needed.
EDIT: I've added the ?
after the [_-]
to account for that being optional, per your comment below.
Some additional information on quantifiers:
{N,}
means that there must be at least N characters from the preceding item to match.{N}
means there must be exactly N of the preceding.{N,M}
means there must be at least N, but no more than M.?
means there must be 0 or 1 of the preceding.+
means there must be 1 or more.*
means there must be 0 or more.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With