From fa40f12094bcfbf1f0fed95d5de04215873924b4 Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Mon, 22 Jan 2024 13:34:56 +0200 Subject: [PATCH] #1796 - Strip emails function --- utils/utils.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index 1252424..9bfa177 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -67,6 +67,32 @@ func ValidateEmailAddress(email string) (string, error) { return cleanEmail, nil } +func StripEmail(email string) (strippedEmail string, strippedDomain string) { + // Strip the email address from the + to the @ + // Define a regular expression pattern to match the "+" to "@" part + emailPattern := `(\+.*@)` + // Define the regular expression pattern to match the domain part after "@" + domainPattern := `@(.+)` + + // Compile the regular expression + emailRegex := regexp.MustCompile(emailPattern) + domainRegex := regexp.MustCompile(domainPattern) + + // Replace the matched part with an empty string + strippedEmail = emailRegex.ReplaceAllString(email, "@") + + // Find the first match in the email address + match := domainRegex.FindStringSubmatch(email) + + // Check if a match was found + if len(match) > 1 { + // The domain part (excluding "@") is in the first capture group (index 1) + strippedDomain = match[1] + } + + return strippedEmail, strippedDomain +} + // IsUrlStrict Returns whether a URL is valid in a strict way (Must have scheme and host) func IsUrlStrict(str string) bool { u, err := url.Parse(str) -- GitLab