Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertmryan/235ea2e3455ae2839f5f20106821865a to your computer and use it in GitHub Desktop.
Save robertmryan/235ea2e3455ae2839f5f20106821865a to your computer and use it in GitHub Desktop.
let regex = Regex {
Capture {
ZeroOrMore {
OneOrMore(.word)
"."
}
OneOrMore(.word)
}
"@"
Capture {
OneOrMore {
OneOrMore(.word)
"."
}
Repeat(2...) {
.word
}
}
}
@robertmryan
Copy link
Author

That is a regex that captures simple email address where the TLD has two or more characters.

@robertmryan
Copy link
Author

As an aside, I find Xcode and Playgrounds have problems disambiguating Repeat syntax, so you could alternatively do something ugly like:

let regex = Regex {
    Capture {
        ZeroOrMore {
            OneOrMore(.word)
            "."
        }
        OneOrMore(.word)
    }
    "@"
    Capture {
        OneOrMore {
            OneOrMore(.word)
            "."
        }
        One(.word)
        OneOrMore(.word)
    }
}

@robertmryan
Copy link
Author

I supply the above for the sake of the RegexBuilder question. There are obviously two issues:

  1. While there are no single-character TLDs, there appears to be nothing technically to prohibit it. I personally would suggest removing that “two or more characters” constraint.

  2. The actual requirements of email verification can be found at How can I validate an email address using a regular expression?. That conforms to RFC 5322, I've modified that to include capture group for the user name and the domain:

    let regex = /((?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*"))@((?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))/

So, hopefully that answers the “regex for email” question, but if the question is “how to have RegexBuilder recognize TLD with two or more characters?”, then perhaps that Repeat(2...) {…} pattern is helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment