Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to split email with Regex

What patterns can i use to get each part on an email , for example :

string email = "Edouard.amo@gmail.com"

I wanna get something like this.

string st1 = "Edouard.amo";
string st2 = "gmail";
string st3 = "com";

by using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

string st1 = Regex.Match(email , pattern1, RegexOptions.IgnoreCase).Value;
string st2 = Regex.Match(email , pattern2, RegexOptions.IgnoreCase).Value;
string st3 = Regex.Match(email , pattern3, RegexOptions.IgnoreCase).Value;

Thanks,

>Solution :

You can try using groups while matching:

var match = Regex.Match(email, @"^(?<name>[^@]*)@(?<server>.*)\.(?<domain>[^.]*)$");

if (match.Success) {
  string st1 = match.Groups["name"].Value;
  string st2 = match.Groups["server"].Value;
  string st3 = match.Groups["domain"].Value;

  ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading