views:

26

answers:

2

I have html email that I would like to track click activity. I need to update all hrefs within the email to point back to my server where they can be logged and redirected. Is there an easy way to do this globally using .Net regex?

<a href="http://abc.com"&gt;ABC&lt;/a&gt;

becomes

<a href="http://mydomain.com?uid=123&amp;url=http:/abc.com&gt;ABC&lt;/a&gt;
+1  A: 

Do not use a RegEx to parse HTML - it is not a regular language. See here for some compelling demonstrations.

Use the HTML Agility Pack to parse the HTML and replace the URLs.

Oded
A: 

Try the following code

public string ReplaceLinks(string emailSource) {
    string resultString = null;
    try {
        resultString = Regex.Replace(emailSource, "(<a href=\")(htt)", new MatchEvaluator(ComputeReplacement));
    } catch (ArgumentException ex) {
        // Syntax error in the regular expression
    }
    return resultString;
}

public String ComputeReplacement(Match m) {
    // You can vary the replacement text for each match on-the-fly
    return "$1http://mydomain.com?uid=123&amp;url=$2";
}
Jonathan Stanton
With minor modification it worked great. Thanks.
Lauren