Skip to content

Instantly share code, notes, and snippets.

@sklinkert
Created May 5, 2020 22:25
Show Gist options
  • Save sklinkert/15c7f46c6039601b4eece9ff508c378a to your computer and use it in GitHub Desktop.
Save sklinkert/15c7f46c6039601b4eece9ff508c378a to your computer and use it in GitHub Desktop.
Guess firstname and lastname from mail address
// guessNameFromMail - Examples:
// warren.buffett@gmail.com -> Warren Buffett
// warren-buffett@gmail.com -> Warren Buffett
// warren_buffett@gmail.com -> Warren Buffett
// w-buffett123@gmail.com -> W. Buffett
func guessNameFromMail(mail string) (firstname, lastname string) {
re := regexp.MustCompile(`^([A-Za-z]+)[\-\.\_]([A-Za-z]+)[0-9]*@`)
matches := re.FindAllStringSubmatch(mail, -1)
if len(matches) == 1 && len(matches[0]) == 3 {
firstname := strings.Title(matches[0][1])
lastname := strings.Title(matches[0][2])
if len(firstname) == 1 {
firstname = firstname + "."
}
return firstname, lastname
}
return "", ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment