Reputation: 13
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
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
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