medium +20 pts

Validate Email Regex

Use regular expressions to validate email addresses with strict rules.

Write a function `is_valid_email(email: str) -> bool` that returns `True` if the email is valid according to the following rules: - The email must have exactly one `@` symbol. - The local part (before `@`) must be 1 to 64 characters long. - The local part may contain letters (a-z, A-Z), digits (0-9), and any of these special characters: `! # $ % & ' * + - / = ? ^ _ ` { | } ~` and the dot `.`. - The dot `.` in the local part must not be the first or last character, and must not appear consecutively (i.e., no `..`). - The domain part (after `@`) must be 1 to 255 characters long. - The domain part must consist of dot-separated labels. Each label must contain only letters, digits, and hyphens, and must not start or end with a hyphen. - The top-level domain (the last label) must contain at least one letter (e.g., `.com`, `.org`, `.io`). Use the `re` module. The function should be case-insensitive for the domain part? Note: The local part case sensitivity is not tested, but the rules apply as stated. The input is a non-empty string. You must implement the function exactly as specified. Examples: ```python is_valid_email("user@example.com") # True is_valid_email("a@b.co") # True is_valid_email("user.name@domain.org") # True is_valid_email("user..name@domain.com") # False is_valid_email("user@domain") # False (no TLD? Actually TLD has no letter? domain has no dot, so invalid) is_valid_email("@domain.com") # False is_valid_email("user@domain..com") # False ``` Note: The rules are strict and must be matched exactly. Edge cases like empty string are not tested because the input is non-empty, but the function should handle gracefully (return False).

Constraints

Email length is at most 320 characters. The input is a string. The function should run in O(n) time. Use `re` module.

Example

>>> is_valid_email("user@example.com")
True
>>> is_valid_email("a@b.co")
True
>>> is_valid_email("user.name@domain.org")
True
>>> is_valid_email("user..name@domain.com")
False
>>> is_valid_email("user@domain")
False
>>> is_valid_email("@domain.com")
False
>>> is_valid_email("user@domain..com")
False
20 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Break the regex into local and domain parts, combine with '@'.
For the local part, use a character class for allowed special chars and enforce dot rules.
For domain labels, use a pattern that allows letters, digits, hyphens but not leading/trailing hyphen.
Test your regex on the provided examples before submitting.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.