Home > Enterprise >  how to split email with Regex
how to split email with Regex

Time:11-25

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

string email = "[email protected]"

I wanna get something like this.

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

by using

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,

CodePudding user response:

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;

  ...
}
  • Related