Regex.Replace 方法的性能!
园子里有很多关于去除Html标签的文章。一个常用的经验是使用 Regex.Replace 方法利用正则去替换。这里有一篇使用该方法的文章 C#中如何去除HTML标记 。下面我贴出该方法的代码,见代码清单1-1
代码清单1-1 引用 http://www.cnblogs.com/zoupeiyang/archive/2009/06/22/1508039.html
///
/// 去除HTML标记
///
/// 包括HTML的源码
/// 已经去除后的文字
public static string ReplaceHtmlTag(string Htmlstring)
{
//删除脚本
Htmlstring = Htmlstring.Replace("\r\n", "");
Htmlstring = Regex.Replace(Htmlstring, @"", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"",@"", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"",@"",@"<.*?>",
@"<(.[^>]*)>",@"([\r\n])[\s]+",@"-->",
@"",@"&(quot|#34);",@"&(amp|#38);",
@"&(lt|#60);",@"&(gt|#62);",@"&(nbsp|#160);",
@"&(iexcl|#161);",@"&(cent|#162);",@"&(pound|#163);",
@"&(copy|#169);",@"&(\d+);"
};
string[] replacement = new string[]
{
"","","","","","","","\"","&","<",">","","\xa1","\xa2","\xa3","\xa9",""
};
#endregion
if (pattern.Length != replacement.Length)
{
throw new Exception("正则表达式数组和替换后的字符数组的长度不一致!");
}
int count = 0; //计数器
foreach (string str in pattern)
{
Regex aRegex = new Regex(str);
aReplaceHtml.AddRegex(aRegex, replacement[count]);
count += 1;
}
return aReplaceHtml;
}
///
/// 增加一个Regex对象
///
/// Regex 对象
/// 该对象对应的替换字符串
private void AddRegex(Regex aRegex, string Replacement)
{
_regexs.Add(aRegex);
_replacement.Add(Replacement);
}
}
}
/// 去除HTML标记
///
/// 包括HTML的源码
/// 已经去除后的文字
public static string ReplaceHtmlTag(string Htmlstring)
{
//删除脚本
Htmlstring = Htmlstring.Replace("\r\n", "");
Htmlstring = Regex.Replace(Htmlstring, @"", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"",@"", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"",@"",@"<.*?>",
@"<(.[^>]*)>",@"([\r\n])[\s]+",@"-->",
@"",@"&(quot|#34);",@"&(amp|#38);",
@"&(lt|#60);",@"&(gt|#62);",@"&(nbsp|#160);",
@"&(iexcl|#161);",@"&(cent|#162);",@"&(pound|#163);",
@"&(copy|#169);",@"&(\d+);"
};
string[] replacement = new string[]
{
"","","","","","","","\"","&","<",">","","\xa1","\xa2","\xa3","\xa9",""
};
#endregion
if (pattern.Length != replacement.Length)
{
throw new Exception("正则表达式数组和替换后的字符数组的长度不一致!");
}
int count = 0; //计数器
foreach (string str in pattern)
{
Regex aRegex = new Regex(str);
aReplaceHtml.AddRegex(aRegex, replacement[count]);
count += 1;
}
return aReplaceHtml;
}
///
/// 增加一个Regex对象
///
/// Regex 对象
/// 该对象对应的替换字符串
private void AddRegex(Regex aRegex, string Replacement)
{
_regexs.Add(aRegex);
_replacement.Add(Replacement);
}
}
}
该类的使用如下,见代码清单1-7
代码清单1-7
public static string ReplaceHtmlTag2(string Htmlstring)
{
return ReplaceHtml.Instance.ReplaceHtmlTag(Htmlstring);
}
{
return ReplaceHtml.Instance.ReplaceHtmlTag(Htmlstring);
}
写到这里让我们来测试一下,2种方法在性能的差距。经过测试,在重复执行 ReplaceHtmlTag 方法和ReplaceHtmlTag2 方法 10,100,1000 次后,性能相差在 2-15陪左右。具体见图1-1
图1-1 2种方法执行 1000 次所消耗的时间对比
说明:该方法在处理短字符串时,性能差距很大。我用新浪的首页做过测试,2种方法的性能只相差1倍。附上源代码,感兴趣的读者可自行测试!:-)
这里下载: RegexTest.rar
End.
0
0