In this challenge, you will implement a text processing utility that uses regular expressions to extract, validate, and transform data from various text formats.
Your task is to create a regular expression processor that can:
- Extract specific data patterns from text (emails, phone numbers, dates, etc.)
- Validate if input strings match specific formats
- Replace or transform text based on pattern matching
- Parse structured text like logs or CSV data
You will need to implement the following functions:
// ExtractEmails extracts all valid email addresses from a text
func ExtractEmails(text string) []string
// ValidatePhone checks if a string is a valid phone number in format (XXX) XXX-XXXX
func ValidatePhone(phone string) bool
// MaskCreditCard replaces all but the last 4 digits of a credit card number with "X"
// Example: "1234-5678-9012-3456" -> "XXXX-XXXX-XXXX-3456"
func MaskCreditCard(cardNumber string) string
// ParseLogEntry parses a log entry with format:
// "YYYY-MM-DD HH:MM:SS LEVEL Message"
// Returns a map with keys: "date", "time", "level", "message"
func ParseLogEntry(logLine string) map[string]string
// ExtractURLs extracts all valid URLs from a text
func ExtractURLs(text string) []string- Input:
"Contact us at support@example.com or sales@company.co.uk for more info." - Output:
["support@example.com", "sales@company.co.uk"]
- Input:
"(555) 123-4567" - Output:
true - Input:
"555-123-4567" - Output:
false
- Input:
"1234-5678-9012-3456" - Output:
"XXXX-XXXX-XXXX-3456" - Input:
"1234567890123456" - Output:
"XXXXXXXXXXXX3456"
- Input:
"2023-11-15 14:23:45 INFO Server started on port 8080" - Output:
map[string]string{
"date": "2023-11-15",
"time": "14:23:45",
"level": "INFO",
"message": "Server started on port 8080",
}- Input:
"Visit https://golang.org and http://example.com/page?q=123 for more information." - Output:
["https://golang.org", "http://example.com/page?q=123"]
- Your solution should handle edge cases appropriately
- Regular expressions should be efficient and avoid excessive backtracking
- Compile regular expressions once and reuse them for better performance
- For email validation, use a reasonable regex that covers common email formats
- Correctness: Does your solution handle all the required cases?
- Efficiency: Are your regular expressions optimized?
- Code Quality: Is your code well-structured and documented?
- Error Handling: Does your code handle invalid inputs gracefully?
See the learning.md document for a comprehensive guide on using regular expressions in Go.
- The
regexppackage in Go provides comprehensive regular expression functionality - Use
MustCompilefor patterns you know are valid to simplify error handling - Remember to handle special characters in your patterns
- For complex patterns, consider breaking them down into smaller parts
- Test your regexes with a variety of inputs, including edge cases