1
\$\begingroup\$

I have the below regex to match email address with atleast one @ and one .

[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}

Am wondering can it be simplified further to match email address with atleast one @ and one .?

\$\endgroup\$
2
\$\begingroup\$

Simple Regular Expression for Email Addresses

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

is one of the simplest possible. In many cases this simple expression is good enough. It consists of five parts:

[A-Z0-9._%+-]+ - the first part of mail address may contain all characters, numbers, points, underscores, percent, plus and minus.

@ - the @ character is mandatory

[A-Z0-9.-]+ - the second part of mail address may contain all characters, numbers, points, underscores.

\. - the point is mandatory

[A-Z]{2,4} - the domain name may contain all characters. The number of characters is limited between 2 and 4.

OPTION 2:

^(.+)@(.+)$ 

This one is simplest and only cares about ‘@’ symbol. Before and after ‘@’ symbol, there can be any number of characters.if you want to validate point(.) add \. into regular expression.

\$\endgroup\$
  • 1
    \$\begingroup\$ FYI, your {2,4} limit is legacy... for example, see: annefrank.house.museum \$\endgroup\$ – rolfl Jan 9 '15 at 18:58
2
\$\begingroup\$

There are many edge cases to e-mail address structure that you will exclude if you try the simplest approach.

One I use that accepts most of the reasonably acceptable e-mail address looks like:

^(?:[w!#$%&'*+-/=?^`{|}~]+.)*[w!#$%&'*+-/=?^`{|}~]+@(?:(?:(?:[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]?)|(?:[(?:(?:[01]?d{1,2}|2[0-4]d|25[0-5]).){3}(?:[01]?d{1,2}|2[0-4]d|25[0-5])]))$

which is preferable to the supposedly correct one:

(?<user>(?:(?:[^ \t\(\)\<\>@,;\:\\\"\.\[\]\r\n]+)|(?:\"(?:(?:[^\"\\\r\n])|(?:\\.))*\"))(?:\.(?:(?:[^ \t\(\)\<\>@,;\:\\\"\.\[\]\r\n]+)|(?:\"(?:(?:[^\"\\\r\n])|(?:\\.))*\")))*)@(?<domain>(?:(?:[^ \t\(\)\<\>@,;\:\\\"\.\[\]\r\n]+)|(?:\[(?:(?:[^\[\]\\\r\n])|(?:\\.))*\]))(?:\.(?:(?:[^ \t\(\)\<\>@,;\:\\\"\.\[\]\r\n]+)|(?:\[(?:(?:[^\[\]\\\r\n])|(?:\\.))*\])))*)

Note that all of the following addresses are perfectly acceptable as e-mail addresses:

        "Someone@Somewhere.co.uk",
        "Somebody{MiddleName}Surname@Somewhere.co.uk",
        "abc.\"defghi\".xyz@example.com",
        "Someone@[192.168.0.1]",
        "niceandsimple@example.com",
        "simplewith+symbol@example.com",
        "less.common@example.com",
        "a.little.more.unusual@dept.example.com",
        "user@[IPv6:2001:db8:1ff::a0b:dbd0]",
        "\"much.more unusual\"@example.com",
        "\"very.unusual.@.unusual.com\"@example.com",
        "\"very.(),:;<>[]\\\".VERY.\\\"very@\\ \\\"very\\\".unusual\"@strange.example.com",
        "0@a",
        "!#$%&'*+-/=?^_`{}|~@example.org",
        "\"()<>[]:,;@\\\\\\\"!#$%&'*+-/=?^_`{}| ~  ? ^_`{}|~.a\"@example.org",
        "\"\"@example.org",
        "postbox@com",

The first regex only accepts some of these - only the second accepts all.

See here for a very good narrative on the subject.

\$\endgroup\$

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.