Last active
August 13, 2023 17:09
-
-
Save LordGhostX/7999cee1064efab7fdf5c290b0bc35b6 to your computer and use it in GitHub Desktop.
RegEX to validate email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# validate email addresses | |
def validate_email(email): | |
pattern = r"(^(?!-|\.)([a-zA-Z0-9._%+-]+)@(?!-)[a-zA-Z0-9.-]+(?<=[a-zA-Z0-9])\.[a-zA-Z]{2,}$)" | |
if re.match(pattern, email): | |
return True | |
else: | |
return False | |
if __name__ == "__main__": | |
# test email validation | |
test_cases = [ | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("[email protected]", True), | |
("email.example.com", False), | |
("email@", False), | |
("@example.com", False), | |
("my [email protected]", False), | |
("email@exa#mple.com", False), | |
("email@[email protected]", False), | |
("[email protected]", False), | |
("[email protected]", False), | |
("email@example", False), | |
("[email protected]", False), | |
("[email protected]", False), | |
("[email protected]", False) | |
] | |
for case in test_cases: | |
assert validate_email(case[0]) is case[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment