0
import re

def validate_email(email):
    pattern = r'^(?:(?:[a-zA-Z0-9_-]+\.)*[a-zA-Z0-9_-]+@)?(?:[a-zA-Z0-9_-]+\.)?example\.com$'
    return re.match(pattern, email)

emails = [
    "john.doe@example.com",
    "jane.smith@subdomain.example.com",
    "user@gmail.com",
    "admin@subdomain.example.org",
]

for email in emails:
    if validate_email(email):
        print(f"{email} is a valid email.")
    else:
        print(f"{email} is NOT a valid email.")

In this code, the regular expression is designed to match email addresses that belong to the domain example.com and can include optional subdomains. It should handle variations like user@example.com, user@sub.example.com, and user@sub.sub.example.com. but its not working as expected

prabu naresh
  • 405
  • 1
  • 10

0 Answers0