Skip to content

Instantly share code, notes, and snippets.

@LordGhostX
Last active August 13, 2023 17:09
Show Gist options
  • Save LordGhostX/7999cee1064efab7fdf5c290b0bc35b6 to your computer and use it in GitHub Desktop.
Save LordGhostX/7999cee1064efab7fdf5c290b0bc35b6 to your computer and use it in GitHub Desktop.
RegEX to validate email
# 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