Skip to content

Instantly share code, notes, and snippets.

@przmv
Created July 2, 2020 13:54
Show Gist options
  • Save przmv/7e46c7d7e2ec46d989c38b43d600651d to your computer and use it in GitHub Desktop.
Save przmv/7e46c7d7e2ec46d989c38b43d600651d to your computer and use it in GitHub Desktop.
Check if lines of the provided files are valid email addresses. Regular expression for checking emails is from the awsome book “Let's Go!” by Alex Edwards — https://lets-go.alexedwards.net/
package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
var EmailRX = 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 main() {
for _, s := range os.Args[1:] {
f, err := os.Open(s)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
defer f.Close()
scanner := bufio.NewScanner(f)
line := 1
for scanner.Scan() {
addr := scanner.Text()
if !EmailRX.MatchString(addr) {
fmt.Fprintf(os.Stderr, "error in %s +%d: %q isn't a valid email address\n", f.Name(), line, addr)
}
line++
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment