Go Regex Validator: Email Address Checker
This challenge focuses on practical application of Go's regexp package to validate data. You will be tasked with creating a robust email address validator using regular expressions in Go. This skill is fundamental for data sanitation and user input validation in many applications.
Problem Description
Your goal is to write a Go function that takes a string as input and returns true if the string is a valid email address, and false otherwise. You must use Go's built-in regexp package to achieve this validation.
Key Requirements:
- Email Validation Logic: Implement a regular expression that adheres to common email address formats. A valid email address generally consists of a local part, an "@" symbol, and a domain part.
- Local Part: Can contain alphanumeric characters, periods (
.), hyphens (-), and plus signs (+). It cannot start or end with a period. Consecutive periods are not allowed. - Domain Part: Consists of one or more labels separated by periods. Each label can contain alphanumeric characters and hyphens. Labels cannot start or end with a hyphen. The top-level domain (TLD) must be at least two characters long and consist of letters only.
- Local Part: Can contain alphanumeric characters, periods (
- Go
regexpPackage Usage: You must use theregexppackage for matching. - Function Signature: The function should have the signature
func IsValidEmail(email string) bool. - Case Insensitivity: The validation should be case-insensitive.
Expected Behavior:
The function should accurately identify valid and invalid email addresses according to the specified criteria.
Edge Cases to Consider:
- Empty strings.
- Strings with leading/trailing whitespace.
- Emails with unusual but technically valid characters (e.g.,
+in the local part). - Emails with invalid domain structures (e.g., no TLD, invalid characters in TLD).
- Emails with consecutive dots or dots at the start/end of the local part.
Examples
Example 1:
Input: "test.email+alias@example.com"
Output: true
Explanation: This is a valid email address. The local part "test.email+alias" is valid, and the domain "example.com" is valid with a TLD "com".
Example 2:
Input: "invalid-email@domain"
Output: false
Explanation: This is an invalid email address because the domain part is missing a top-level domain (e.g., ".com", ".org").
Example 3:
Input: "another..email@domain.co.uk"
Output: false
Explanation: This is an invalid email address because the local part contains consecutive periods.
Example 4:
Input: " leading.space@domain.com"
Output: false
Explanation: This is an invalid email address due to leading whitespace. (Note: If you decide to trim whitespace before validation, this would be true. The problem statement implies exact string matching for validity unless specified otherwise, but consider this as a point for discussion).
Example 5:
Input: "user@domain-.com"
Output: false
Explanation: This is an invalid email address because the domain label ends with a hyphen.
Constraints
- The input
emailstring will be at most 254 characters long. - The input
emailstring will consist of printable ASCII characters. - The regular expression should be compiled once for efficiency if the function is called multiple times.
Notes
- While there are many RFC specifications for email addresses, this challenge focuses on a common, practical subset of valid formats. Aim for a regex that covers most everyday email addresses.
- Consider how to handle potential errors during regex compilation (though for this challenge, you can assume the regex will be valid).
- Think about the order of operations: Should you trim whitespace before applying the regex? The problem description implies strict validation of the input string as-is.
- A good starting point for a regex might involve capturing the local part, the "@" symbol, and the domain part separately.