3

I have seen the below pattern is used in several places (even on sof) as an example for email id validation.

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b

the above is taken from https://www.regular-expressions.info/tutorial.html, and the quote

 this pattern describes an email address. 

This pattern does not take into consideration lower case alphabets (unless I am missing something).

Is there any thing further I got to understand about this patter? As this pattern can not be really used in production? Why is it so popular?

  • 3
    See How to validate an email address using a regular expression? to understand what’s involved in checking the format of an email address (let alone validating it). You need PCRE or an equivalent regex engine to handle the grammar defined in RFC5322; alternatively, you can use a parser. – Stephen Kitt Sep 15 at 8:14
  • 3
    The title doesn't match the body, and is probably why people are thinking that this is opinion-based. The title invites the response Well how on Earth should we know what goes through people's heads when they copy this stuff?. Whereas the body asks the better and quite different question of whether this regular expression can be used in production. – JdeBP Sep 15 at 8:26
  • 2
    Email addresses are not case-sensitive (with the possible exception of something from the 1980s -- UseNET?). You are probably expected to run the address through toupper() as a first step. – Paul_Pedant Sep 15 at 8:37
  • 3
    No, it's only a MAY and a SHOULD. Only "Postmaster" MUST be case-insensitive. – JdeBP Sep 15 at 8:52
  • 27
    The very sentence you quote from the linked page includes a link to a page that states, among other things, "This regex is intended to be used with your regex engine’s “case insensitive” option turned on. (You’d be surprised how many “bug” reports I get about that.)" – fra-san Sep 15 at 9:41
14

I have seen the below pattern is used in several places (even on sof) ... Why is it so popular?

Because people are copy-pasting the first google search result in their answers, blogs and code, which are in turn picked up by search engines, which brings even more people to copy-paste it, generating an infernal vortex which finishes by driving off the internet any better content.

unless I am missing something

Following the link from your question there's a long rambling digression which should "answer" all your questions.

| improve this answer | |
  • 2
    This quote from the digression pretty much sums it up: "The virtue of my regular expression above is that it matches 99% of the email addresses in use today." - So it's OK to refuse 1% of people with valid email addresses from your site? To hell with that, it's just making excuses for a crappy RE. Or really, for using the wrong tool for the job (which REs so so often are). – marcelm Sep 15 at 21:22
  • The long rambling digression includes the following: This regex is intended to be used with your regex engine’s “case insensitive” option turned on. (You’d be surprised how many “bug” reports I get about that.) – Barmar Sep 16 at 15:58
  • @marcelm Thank you. My work email uses the apostrophe to the left of the @. This has caused innumerable headaches with web sites saying I have an invalid email address. Even large tech companies have problems with my email. – doneal24 Sep 16 at 16:58
12

It should not be used in production. For example "email me"@contoso.com is a syntactically valid email address but will not be matched by that naïve RE.

See RFC5322 section 3.4.1 for the definitive grammar.

Annoyingly perhaps, there is no BRE or ERE that can match that grammar definition, but you can get very close. However, a PCRE will do the trick. See How to validate an email address using a regular expression? on StackOverflow.

| improve this answer | |
  • 4
    Even if you use the RFC regex, you must validate the email address in a second way. Simply because that user might have a typo when he enters it. So why use a regex more complicated than .*@.*\..* at all? – Thomas Weller Sep 15 at 20:18
  • 3
    @ThomasWeller And even that's actually too complicated, since the host part could technically be a TLD, e.g. thomas@de. With gTLDs, that actually becomes a viable option, too. (The host part could even be an IP address, but I don't mind those being rejected.) – marcelm Sep 15 at 21:17
  • 1
    Absolutely. My recommendation would be to try sending to it. Either it'll work (in which case it's potentially valid) or it won't. If it's potentially valid you need some way to verify it's the user's address and not someone picked at random – roaima Sep 15 at 21:22
  • 2
    @marcelm newgTLDs are not allowed to do that. It was an option for traditional TLDs. Time ago .io did provide such emails but it stopped doing so, and I don't think any top level tld does that. – Ángel Sep 15 at 23:04
  • 1
    @marcelm see on the Registry agreement the "DNS Service – TLD Zone Contents" section, it even clarifies at the end "The above language effectively does not allow, among other things, the inclusion of DNS resource records that would enable a dotless domain name (e.g., apex A, AAAA, MX records) in the TLD zone.)" Although it might be approved through a "Registry Services Evaluation Process (RSEP)" – Ángel Sep 17 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.