我有html电子邮件,我想跟踪点击活动。我需要更新电子邮件中的所有href,以指回我的服务器,在那里可以记录和重定向。有没有一种简单的方法可以使用.Net regex在全球范围内完成这项工作?
<a href="http://abc.com">ABC</a>
变为
<a href="http://mydomain.com?uid=123&url=http:/abc.com>ABC</a>
答案 0 :(得分:2)
不要使用RegEx来解析HTML - 它不是常规语言。有关引人注目的演示,请参阅here。
使用HTML Agility Pack解析HTML并替换网址。
答案 1 :(得分:-1)
尝试以下代码
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";
}