Aruna
Aruna

Reputation: 2052

How to split recipient name and email address using regular expressions in C#?

I have a string array which contains email recipients' names and emails. To send the relevant emails I need to split the values using regular expression. I'm new to this regex area:

Here is the string array

[ 
 "Jason D Silva <[email protected]>",
 "Aruna Nishantha <[email protected]>",
 "Dan Carter <[email protected]>"
]

I want to split values into Name and Email. Once the split is done correctly i'm going to insert that to a list as follows:

List<KeyValuePair<string, string>> recipientList = new List<KeyValuePair<string, string>>();
  foreach (var item in recipients)
    {
        // Regex pattern 
        if (true)
         {
             //add to recipientList
         }
     }

Any help for regex pattern pls?

Upvotes: 0

Views: 1443

Answers (3)

Kavindu Dodanduwa
Kavindu Dodanduwa

Reputation: 13077

A proper implementation with String.Substring method

int start, end;

String[] a = {"Jason D Silva <[email protected]>","Aruna Nishantha <[email protected]>","Dan Carter <[email protected]>" };

 foreach (String item in a)
 {
       Console.WriteLine(item);

       end = item.IndexOf('<');

       Console.WriteLine(" \t Name : " + item.Substring(0, end));

       start = item.IndexOf('<');
       end = item.IndexOf('>');

       Console.WriteLine(" \t email : " + item.Substring(start + 1, end - (start + 1)));

}

Output :

"Jason D Silva <[email protected]>"                                                                                                                                
         Name : Jason D Silva                                                                                                                                     
         email : [email protected]                                                                                                                                  
"Aruna Nishantha <[email protected]>"                                                                                                                            
         Name : Aruna Nishantha                                                                                                                                   
         email : [email protected]                                                                                                                               
"Dan Carter <[email protected]>"                                                                                                                              
         Name : Dan Carter                                                                                                                                        
         email : [email protected]

Upvotes: 1

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37113

You don´t need regex for this, you may just use String.Split instead:

string[] myArray = // ...
foreach(string kv in myArray) 
{
    string[] values = kv.Split(" <");
    // to eliminate the terminating ("noisy" > at the end
    values[1] = values[1].Substring(0, values[1].IndexOf('>');
}

This of course assumes that you strings will always look the same.

Upvotes: 0

Thomas Lielacher
Thomas Lielacher

Reputation: 1067

You could use following regular expression:

^(?<Name>.*)\s\<(?<Email>.*)\>$

With this you can retrieve the name and the e-mail address with:

var match = Regex.Match("Jason D Silva <[email protected]>", @"^(?<Name>.*)\s\<(?<Email>.*)\>$");
var email = match.Groups["Email"].Value;
var name = match.Groups["Name"].Value;

Upvotes: 9

Related Questions