From 60a3a796cdc863b35e1e9051bfe95e1fc7dfe4e0 Mon Sep 17 00:00:00 2001 From: Bushra Tayyab Date: Thu, 8 Jan 2026 11:20:11 +0530 Subject: [PATCH] Refactor email validation and improve readability Improved email validation using a cleaner regex, added docstrings, and separated email parsing logic for better readability and maintainability. --- Email Slicer/EmailSlicer.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/Email Slicer/EmailSlicer.py b/Email Slicer/EmailSlicer.py index 54171bcf..d7930272 100644 --- a/Email Slicer/EmailSlicer.py +++ b/Email Slicer/EmailSlicer.py @@ -1,21 +1,26 @@ import re -def isValidEmail(email): +def is_valid_email(email): + """ + Checks whether the given email address is valid. + Returns True if valid, otherwise False. + """ + regex = r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$' + return re.fullmatch(regex, email) is not None - # Regular expression for validating an Email - regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' - - if(re.fullmatch(regex, email)): - return True - else: - return False -email = input("Enter your email id - ") +def extract_email_parts(email): + """ + Extracts username and domain from a valid email. + """ + return email.split('@', 1) -if isValidEmail(email): - username = email[0:email.index('@')] - domain = email[email.index('@')+1: ] - print("Username - ", username) - print("Domain - ", domain) + +email = input("Enter your email id: ") + +if is_valid_email(email): + username, domain = extract_email_parts(email) + print("Username:", username) + print("Domain:", domain) else: - print("Invalid Email!") \ No newline at end of file + print("Invalid Email!")