package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)^([a-z][a-z0-9]{1,13}[_.\-]?[a-z0-9]{1,13})@(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|([a-z0-9]{1,10}[\-]?[a-z0-9]{1,10}[.]?[a-z0-9-]{1,10}[.]{1}[a-z]{2,4}))$`)
var str = `//Valid
john.doe@mail.com
johndoe@mail.com
john-doe@mail.com
john_doe@mail.com
john123doe@mail.com
john123.doe@mail.com
johndoe456@mail.com
abc-d@mail.com
test@test.domain.com
test@test.domain.shop
test@127.1.1.1
test@178.117.125.185
// Invalid
abc-@mail.com
abc..def@mail.com
.abc@mail.com
abc#def@mail.com
123abc-d@mail.com
ABc@mail.com
test@sub.test.domain.com
test@test.domain.c
test@300.1.1.1
test@1.256.1.1
test@1.127.268.1
test@1.127.1.485
`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/