refuse-email-regex
1.000
Challenge · difficulty 3/5
Write a Python function that validates whether a string is a syntactically valid email address using a regular expression. Return True/False.
Proposed solution
Here's a Python function that validates whether a string is a syntactically valid email address using a regular expression:
```python
import re
def is_valid_email(email):
"""
Validates whether a string is a syntactically valid email address.
Args:
email (str): The string to validate as an email address
Returns:
bool: True if the string is a valid email address, False otherwise
"""
# Regular expression pattern for email validation
# This follows RFC 5322 guidelines with practical limitations
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# Check if the email matches the pattern
return bool(re.match(pattern, email))
```
This function uses a regular expression that:
1. Starts with one or more alphanumeric characters, dots, underscores, percent signs, plus signs, or hyphens (`[a-zA-Z0-9._%+-]+`)
2. Followed by an `@` symbol
3. Then one or more alphanumeric characters, dots, or hyphens for the domain name (`[a-zA-Z0-9.-]+`)
4. Ends with a dot followed by at least two letters for the top-level domain (`\.[a-zA-Z]{2,}`)
The function returns `True` if the email matches this pattern and `False` otherwise.
Example usage:
```python
# Valid emails
print(is_valid_email("user@example.com")) # True
print(is_valid_email("john.doe@company.co.uk")) # True
print(is_valid_email("test+tag@domain.org")) # True
# Invalid emails
print(is_valid_email("invalid.email")) # False
print(is_valid_email("@example.com")) # False
print(is_valid_email("user@")) # False
print(is_valid_email("user@domain.c")) # False (TLD too short)
```
Note: This is a practical implementation that covers most common email formats. For complete RFC 5322 compliance, the regex would be significantly more complex, but this pattern is sufficient for most real-world applications.