如何从 Golang 中的长字符串中解析电子邮件地址

如何从 Golang 中的长字符串中仅提取电子邮件地址?例如:


"a bunch of irrelevant text fjewiwofjfjvnvkdlslsosiejwoqlwpwpwo

 mail=jim.halpert@gmail.com,ou=f,c=US

 mail=apple.pie@gmail.com,ou=f,c=US

 mail=hello.world@gmail.com,ou=f,c=US

 mail=alex.alex@gmail.com,ou=f,c=US

 mail=bob.jim@gmail.com,ou=people,ou=f,c=US

 mail=arnold.schwarzenegger@gmail.com,ou=f,c=US"

这将返回所有电子邮件的列表:[jim.halpert@gmail.com, apple.pie@gmail.com, etc...]


每个电子邮件地址都以“mail=”开头,以逗号“,”结尾。


沧海一幻觉
浏览 225回答 4
4回答

墨色风雨

你可以使用 golang 的原始包是 regexp.Compile 或 regexp.MustCompiler, _ := regexp.Compile(regexEmail)    newVariable := `a bunch of irrelevant text fjewiwofjfjvnvkdlslsosiejwoqlwpwpwo mail=jim.halpert@gmail.com,ou=f,c=US mail=apple.pie@gmail.com,ou=f,c=US mail=hello.world@gmail.com,ou=f,c=US mail=alex.alex@gmail.com,ou=f,c=US mail=bob.jim@gmail.com,ou=people,ou=f,c=US mail=arnold.schwarzenegger@gmail.com,ou=f,c=US`    fmt.Printf("%#v\n", r.FindStringSubmatch(newVariable))    fmt.Printf("%#v\n", r.SubexpNames())

隔江千里

你可以使用这个包来做到这一点:https://github.com/hamidteimouri/htutils/blob/main/htregex/htregex.go// Emails finds all email stringsfunc Emails(text string) []string {    return match(text, EmailsRegex)}

开心每一天1111

为此,您需要将 long go string 分解为您需要的部分。您可以使用正则表达式进行过滤和搜索,以匹配您在上面看到的电子邮件模式。这是一段使用正则表达式的代码,首先获取该部分,"mail="然后进一步格式化电子邮件,删除尾随, import (    "fmt"    "regexp"    "strings")func main() {    var re = regexp.MustCompile(`(?m)mail=[A-Za-z.@0-9]+\,`)    var str = `a bunch of irrelevant text fjewiwofjfjvnvkdlslsosiejwoqlwpwpwo mail=jim.halpert@gmail.com,ou=f,c=US mail=apple.pie@gmail.com,ou=f,c=US mail=hello.world@gmail.com,ou=f,c=US mail=alex.alex@gmail.com,ou=f,c=US mail=bob.jim@gmail.com,ou=people,ou=f,c=US mail=arnold.schwarzenegger@gmail.com,ou=f,c=US`    for i, match := range re.FindAllString(str, -1) {        fmt.Println(match, "found at index", i)        email := strings.Split(match, "=")[1]        email = strings.ReplaceAll(email, ",", "")        fmt.Print(email)    }}

尚方宝剑之说

虽然我同意用户 datenwolf 的评论,但这里是另一个不涉及正则表达式的版本。它还处理更复杂的电子邮件格式,包括本地部分中的逗号。使用正则表达式不容易实现的东西。请参阅https://stackoverflow.com/a/2049510/11892070import (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings")var str = `a bunch of irrelevant text fjewiwofjfjvnvkdlslsosiejwoqlwpwpwomail=jim.halpert@gmail.com,ou=f,c=USmail=apple.pie@gmail.com,ou=f,c=USmail=hello.world@gmail.com,ou=f,c=USmail=alex.alex@gmail.com,ou=f,c=USmail=bob.jim@gmail.com,ou=people,ou=f,c=USmail=arnold.schwarzenegger@gmail.com,ou=f,c=USmail=(comented)arnold.schwarzenegger@gmail.com,ou=f,c=USmail="(with comma inside)arnold,schwarzenegger@gmail.com",ou=f,c=USmail=nocommaatall@gmail.com`func main() {&nbsp; &nbsp; var emails []string&nbsp; &nbsp; sc := bufio.NewScanner(strings.NewReader(str))&nbsp; &nbsp; for sc.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; t := sc.Text()&nbsp; &nbsp; &nbsp; &nbsp; if !strings.HasPrefix(t, "mail=") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; t = t[5:]&nbsp; &nbsp; &nbsp; &nbsp; // Lookup for the next comma after the @.&nbsp; &nbsp; &nbsp; &nbsp; at := strings.Index(t, "@")&nbsp; &nbsp; &nbsp; &nbsp; comma := strings.Index(t[at:], ",")&nbsp; &nbsp; &nbsp; &nbsp; if comma < 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email := strings.TrimSpace(t)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; emails = append(emails, email)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; comma += at&nbsp; &nbsp; &nbsp; &nbsp; email := strings.TrimSpace(t[:comma])&nbsp; &nbsp; &nbsp; &nbsp; emails = append(emails, email)&nbsp; &nbsp; }&nbsp; &nbsp; for _, e := range emails {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(e)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go