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.