I have an email address
[email protected]
I want to get the domain name from the email address. Can I achieve this with Regex?
I have an email address
[email protected]
I want to get the domain name from the email address. Can I achieve this with Regex?
Using MailAddress you can fetch the Host
from a property instead
MailAddress address = new MailAddress("[email protected]");
string host = address.Host; // host contains yahoo.com
If Default's answer is not what you're attempting you could always Split
the email string after the '@'
string s = "[email protected]";
string[] words = s.Split('@');
words[0]
would be xyz
if you needed it in future
words[1]
would be yahoo.com
But Default's answer is certainly an easier way of approaching this.
Or for string based solutions:
string address = "[email protected]";
string host;
// using Split
host = address.Split('@')[1];
// using Split with maximum number of substrings (more explicit)
host = address.Split(new char[] { '@' }, 2)[1];
// using Substring/IndexOf
host = address.Substring(address.IndexOf('@') + 1);
Simple Substring Method will do the trick here
string emailAddress = @"[email protected]"; string domainName = emailAddress.Substring(emailAddress.IndexOf('.',emailAddress.LastIndexOf('@')));
Console.WriteLine (domainName);
Or If you have have bit of money you can get this library and that will do the work for you
https://afterlogic.com/mailbee-net/docs/MailBee.Mime.EmailAddress.GetDomain.html
© 2022 - 2024 — McMap. All rights reserved.