In: Computer Science
PYTHON
Write a regular expression that will accept any properly formatted email address, and reject any invalid email address. An example of a valid inputted email address would be "[email protected]".
# Python program to validate an Email
# re module provides support
# for regular expressions
import re
# Make a regular expression
# for validating an Email
regex = '^[a-zA-Z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
# for custom mails use: '^[a-zA-Z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w+$'
# Define a function for
# for validating an Email
def check(email):
# pass the regular expression
# and the string in search() method
if(re.search(regex,email)):
print("Valid Email")
else:
print("Invalid Email")
# Driver Code
if __name__ == '__main__' :
# Enter the email
email = input("Enter your email : ")
check(email)
Output :