← Back to articles

C# Regex for verifying e-mails

The famous emailregex.com example is horrible, so I made my own C# email regex — loosely based on it, compiled, and good enough for most use cases.

Tedd

Regex for matching e-mail in C#. Came across http://emailregex.com/ which has a horrible example, so ended up making my own loosely based on that.

It leaves much to be desired, but will cover most use cases well enough.

public static readonly Regex _emailRegex = new Regex(
@"^[\\\w0-9!#$%&'*+/=?^_`{|}~-]{1,50}(?:\\\\.[\\\w0-9!#$%&'*+/=?^_`{|}~-]{1,50}){0,8}"
+ @"@(?:[-\\\w0-9]{1,50}?\\\\.){1,10}[-\\\w0-9]{2,50}?$",
RegexOptions.Compiled); 

Written by Tedd · Oslo, Norway

Updated 5 Jan 2017

Follow the journal via RSS