user5368153
user5368153

Reputation:

Python - Regex not working with a list of invalid characters?

I'm making a validator for an email for a project.

  email = input("Enter an email address: ")

  if re.match("[~!#$%^&*()_+{}:;\']+$", email):
        print("Test 7 - Failed! The email has invalid characters!")
        test7 = "failed"
    else:
        print("Test 7 - Passed! The email has no invalid characters!")
        test7 = "passed"

If I enter something like anyemail£()@gmail.com then it still says that it is valid? I know it must be an issue with the re.match, however could someone explain the problem?

I also tried using a list and using the find command to find the particular invalid characters.

Upvotes: 1

Views: 1579

Answers (2)

joechild
joechild

Reputation: 111

In addition to the reason stribizhev suggested, some of those characters have meaning when used in a regular expression, which means you would need to escape them with a backslash (it's a little hard to tell, for instance, if you are allowed backslashes in your email based on the above regex)

If you are just looking for invalid characters, it's much easier to do it without regex. For example, you could do something like this:

invalid_chars = r"\~!#$%^&*()_+{}:;"
for char in email:
    if char in invalid_chars:
        # fail the test
        pass

However, I would recommend using a whitelist of characters for this. There are a lot of strange characters out there.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You are checking if the whole string consists of the special characters (since re.match is searching for a pattern match at the beginning of the string and you have $ end-of-string anchor at the end of your pattern).

Remove the +$ and use re.search to check if a string (email) contains at least one special character.

import re
email = 'anyemail£()@gmail.com'
if re.search("[~!#$%^&*()_+{}:;\']", email):
    print("Test 7 - Failed! The email has invalid characters!")
    test7 = "failed"
else:
    print("Test 7 - Passed! The email has no invalid characters!")
    test7 = "passed"
# ==> Test 7 - Failed! The email has invalid characters!

See IDEONE demo

Upvotes: 3

Related Questions