Parsing Email
As with all advice, feel free to drop by and suggest a better library / module! REL makes no claims that these are the best modules, just a few that seem to do the job from some quick searching.
What’s better than a gigantic regex to try to parse an email? Actually parsing an email the right way. It takes some RFCs, some patience, and tens to hundreds to thousands of revisions along the way as you learn all of the different ways that the standard is a non-standard.
Python3 - The Batteries were Included⌗
Your version may be different, but at a quick glance, it looks like Ben Escoto’s version has been around for a while, with RFC2822 support. The code hints that you’re supposed to reach it through the email.utils interface instead of reaching for the underlying _parseaddr.py class directly.
>>> import email.utils
>>> email.utils.parseaddr("The Good User <trynottoregex@regexlicensing.org>")
('The Good User', 'trynottoregex@regexlicensing.org')
Golang⌗
Go claims to have had support since 1.1 from the docs, and claims RFC5322 support.
package main
// based on the godoc example
import (
"fmt"
"net/mail"
)
func main() {
parsedEmail, err := mail.ParseAddress("The Good User <trynottoregex@regexlicensing.org>")
if err != nil {
panic(err)
}
fmt.Printf("Name [%s] Address [%s]\n",
parsedEmail.Name, parsedEmail.Address)
}
Javascript⌗
Jack Bearheart starts the email-addresses js repo with: Want to see if something could be an email address? Want to grab the display name or just the address out of a string? Put your regexes down and use this parser! [emphasis added]
Typescript bindings are included, the minified version is 14KB according to GH.
While the code isn’t fully regex free (perhaps there isn’t a faster way to collapse ws in js), the RFC commitment and cheeky tone may be enough to have a license be issued.
const ea = require("email-addresses");
const parsed = ea.parseOneAddress(
"The Good User <trynottoregex@regexlicensing.org>");
console.log("Result:", parsed);