Created
June 5, 2024 14:54
-
-
Save robertmryan/235ea2e3455ae2839f5f20106821865a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let regex = Regex { | |
Capture { | |
ZeroOrMore { | |
OneOrMore(.word) | |
"." | |
} | |
OneOrMore(.word) | |
} | |
"@" | |
Capture { | |
OneOrMore { | |
OneOrMore(.word) | |
"." | |
} | |
Repeat(2...) { | |
.word | |
} | |
} | |
} |
I supply the above for the sake of the RegexBuilder
question. There are obviously two issues:
-
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.
-
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
As an aside, I find Xcode and Playgrounds have problems disambiguating
Repeat
syntax, so you could alternatively do something ugly like: