D-W D-W - 1 year ago 71
C# Question

How to extract email from html link

Hi I have a csv file which I need to format (columns) email, they are in the csv as follows

<a href=\mailto:john@domain.com\">john@domain.com</a>"
<a href=\mailto:dave.h@domain22.co.uk\">dave.h@domain22.co.uk</a>"


So i want to remove
<a href=\mailto:john@domain.com\"> </a>"
and just use john@domain.com

I have the following

foreach (var clientI in clientImportList)
newClient = new DomainObjects.Client();
//Remove unwanted email text??
newClient.Email = clientI.Email

Answer Source

I usually write myself little utility classes and extensions to handle things like this. Since this probably won't be the last time you have to do something like this you could do this:

Create an Extension of the string class:

public static class StringExtensions
    public static string ExtractMiddle(this string text, string front, string back)
        text = text.Substring(text.IndexOf(front) + 1);
        return text.Remove(text.IndexOf(back));

And then do this(Could use better naming, but you get the point):

string emailAddress = text.ExtractMiddle(">", "<");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download