duanchouyi6730 2018-09-14 15:29
浏览 30
已采纳

解析完整的电子邮件地址包含的名称

I need get domain from email address, problem is same email have name included name < email >, my curent code is :

var (
    ErrBadFormat        = errors.New("invalid format")
    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])?)*$")
)

func GetFromDomain(email string) string {
    if !emailRegexp.MatchString(email) {
        fmt.Println("Error %FROMDOMAIN ", email)
        return "localhost"
    }

    i := strings.LastIndexByte(email, '@')
    return email[i+1:]
}

What is the best practice to do this? My method with LastIndexByte return error when email have name included. How I can fix this problem?

  • 写回答

1条回答 默认 最新

  • droxlzcgnr639823 2018-09-14 15:31
    关注

    Your best bet is to use a proper email parsing library, such as the standard library's mail.ParseAddress method.

    From there, you can easily extract the domain name from the address, by splitting on @--but be careful! It's actually legal to have a quoted or escaped @ in the username portion of the email address (i.e. "foo@bar"@example.com), so you must split on the last @.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计