Regular Expression for domain from email address

TimS picture TimS · Sep 28, 2009 · Viewed 29.8k times · Source

Can anyone help me with a regular expression that will return the end part of an email address, after the @ symbol? I'm new to regex, but want to learn how to use it rather than writing inefficient .Net string functions!

E.g. for an input of "[email protected]" I need an output of "example.com".

Cheers! Tim

Answer

Stephan202 picture Stephan202 · Sep 28, 2009

A regular expression is quite heavy machinery for this purpose. Just split the string containing the email address at the @ character, and take the second half. (An email address is guaranteed to contain only one @ character.)