How to Use Regular Expressions to Validate an Email Address

import re

def is_valid_email(email):
    pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
    return re.match(pattern, email) is not None

email_address = "user@example.com"
print(f"Is {email_address} a valid email? {is_valid_email(email_address)}")

Output:

Is user@example.com a valid email? True

Leave a Comment

Your email address will not be published.