I am trying to take a list of eMail addresses along with first and last names and convert them to a CSV format. My eMail addresses are in the following format:
First, Last <[email protected]>; First, Last <[email protected]>;
The output I need is the following:
[email protected],[email protected]
I am using the following code:
string[] addresses = addresses_Delimited.Split(new Char[] { '<', '>' });
addresses_Delimited is my list of addresses in the original format.
The problem is that it is not eliminating first and last names; instead it is returning first and last names as entries in the array addresses. So, addresses[0] = "First, Last", addresses[1] = "[email protected]", and addresses[2] = "; First, Last". All first and last name entries after the first one have a semicolon in them.
How do I make string.Split remove all text outside "<" and ">"? Do I need to use something else?
Rather than using a Split which does not care that the delimiters are paired up, use a regular expression like this:
<([^>]+)>
When you apply this regex to your input strings, you would capture the content of angular brackets into capturing group number 1:
var s = "First, Last <[email protected]>; First, Last <[email protected]>;";
Regex regex = new Regex(@"<([^>]+)>");
foreach (Match m in regex.Matches(s)) {
Console.WriteLine(m.Groups[1]);
}
Demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With