Email Validation with Regular Expressions

MEDIUM

Write a Python function to validate an email address using regular expressions. The function should return True if the email address is valid according to a common pattern, and False otherwise.

A common (simplified) pattern for an email is: username@domain.extension

  • username: Can contain letters (a-z, A-Z), numbers (0-9), periods (.), underscores (_), percent signs (%), plus signs (+), and hyphens (-).
  • domain: Can contain letters, numbers, and hyphens (-). It typically consists of one or more parts separated by dots.
  • extension: The top-level domain (TLD) usually consists of 2 or more letters (e.g., .com, .org, .co.uk).

 

Examples:

Input: "test.user+label@example.com"   Output: True
Input: "user@sub.domain.co.uk"       Output: True
Input: "invalid_email@"               Output: False
Input: "@domain.com"                 Output: False
Input: "user@domain"                 Output: False (missing .extension)
Input: "user@domain.c"               Output: False (extension too short)

Constraints:

  • The input will be a string.

Function Signature (Python):

import re

class Solution:
    def is_valid_email(self, email_address: str) -> bool:
        # Your code here
        pass

 

Nerchuko Academy · Free DS Interview Prep