-1

I have a string which is separated by a "," like string:= "abc@bk.com, cde@bk.com" I want to make a regular expression which will cover all the whitespace after and before the emails or is there another function strings.Replace to replace the whitespace? They both do the same work but I don't know which is better. If the regular expression is better then an you give a example and if strings.Replace function is better then provide an example of that. I have tried a small code on it:-

package main

import (
  "fmt"
  "regexp"
  "strings"
)

type User struct {
  Name []CustomerDetails `json:"name" bson:"name"`
}
type CustomerDetails struct {
  Value             string `json:"value" bson:"value"`
  Note              string `json:"note" bson:"note"`
  SendNotifications bool   `json:"send_notifications" bson:"send_notifications"`
}

func main() {
  var emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
  var custName User
  emails := "abc@bk.com, cde@bk.com"
  splitEmails := strings.Split(emails, ",")
  fmt.Println(splitEmails)
  for _, email := range splitEmails {
    email = strings.Replace(email, " ", "", -1)
    if emailRegexp.MatchString(email) {
        custName.Name = append(custName.Name, CustomerDetails{
            Value: email,
        })
    }
  }
  fmt.Println(custName)
}

This example is based on the function strings.Replace. Can anybody help me to solve this?

Thanks for your precious time.

Nick
  • 138,499
  • 22
  • 57
  • 95
user10665991
  • 73
  • 1
  • 3
  • 10
  • 1
    You **cannot** and you **must** **not** try this: This will fail. Email addresses are *far* to complex to parse with a regexp. Do what Flimzy suggests. – Volker Nov 22 '18 at 08:18

2 Answers2

3

Do not use regular expressions for this. Your current approach, of splitting on white space, will break with valid email addresses that contain whitespace. While it is possible to parse many email addresses with a regular expression, the necessary regular expression is very, very ugly, and even that doesn't handle all corner cases.

Instead, to parse a list of email addresses, you should use the standard library's mail.ParseAddressList function.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • The expression in the linked answer is often quoted, however "the regular expression does not cope with comments in email addresses. The RFC allows comments to be arbitrarily nested. A single regular expression cannot cope with this." http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html. There is also the matter that this is Perl syntax and does not work unmodified in RE2. So yeah, definitely use the parsing functions in the mail package. – Peter Nov 22 '18 at 09:10
  • @Peter: Thanks, I've added a comment to the effect that the mentioned RE isn't complete. – Jonathan Hall Nov 22 '18 at 11:14
-2

You may use .strip() to clear whitespace on both sides.
Or re.split() to split with pattern /,\s*/.

Technobulka
  • 51
  • 1
  • 6