Lauren
Lauren

Reputation: 13

Use Regex to replace HREFs

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">ABC</a>

becomes

<a href="http://mydomain.com?uid=123&url=http:/abc.com>ABC</a>

Upvotes: 1

Views: 171

Answers (2)

Jonathan Stanton
Jonathan Stanton

Reputation: 2660

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&url=$2";
}

Upvotes: -1

Oded
Oded

Reputation: 499382

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.

Upvotes: 2

Related Questions