无论你用什么语言,正则表达式的处理方法都是非常灵活、高效的,尤其是对某些字符串的抓取、过滤方面,更显其优势。
正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的执行效率非常高,运行速度相当快。因此我在项目的开发中,通常把正则表达式作为处理问题的首选方法。
正则表达式的运用,在各种语言里都是相通的,也就是说,当你懂得在PHP中使用正则表达式,那么在任何一种语言中也能轻驾就熟。
这里给出两个asp.net实例。
1、asp.net正则表达式提取网址、标题、图片等
例如,有如下的字符串:
<li><a href="http://www.webkaka.com/blog/archives/how-to-add-links-on-baidu-blog.html" title="怎样在百度空间添加友情链接"><span class="article-date">[14/11]</span>怎样在百度空间添加友情链接</a></li>
现在,需要提取 href 后面的网址,[]内的日期,和 链接的文字。
asp.net的实现方式如下:
string strHTML = "<li><a href=\http://www.webkaka.com/blog/archives/how-to-add-links-on-baidu-blog.html\ title=\"怎样在百度空间添加友情链接\"><span class=\"article-date\">[14/11]</span>怎样在百度空间添加友情链接</a></li>";
string pattern = "http://([^\\s]+)\".+?span.+?\\[(.+?)\\].+?>(.+?)<";
Regex reg = new Regex( pattern, RegexOptions.IgnoreCase );
MatchCollection mc = reg.Matches( strHTML );
if (mc.Count > 0)
{
foreach (Match m in mc)
{
Console.WriteLine( m.Groups[1].Value );
Console.WriteLine( m.Groups[2].Value );
Console.WriteLine( m.Groups[3].Value );
}
}
2、asp.net正则表达式删除HTML代码
public static string NoHTML(string Htmlstring) //替换HTML标记
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", 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, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的执行效率非常高,运行速度相当快。因此我在项目的开发中,通常把正则表达式作为处理问题的首选方法。
正则表达式的运用,在各种语言里都是相通的,也就是说,当你懂得在PHP中使用正则表达式,那么在任何一种语言中也能轻驾就熟。
这里给出两个asp.net实例。
1、asp.net正则表达式提取网址、标题、图片等
例如,有如下的字符串:
<li><a href="http://www.webkaka.com/blog/archives/how-to-add-links-on-baidu-blog.html" title="怎样在百度空间添加友情链接"><span class="article-date">[14/11]</span>怎样在百度空间添加友情链接</a></li>
现在,需要提取 href 后面的网址,[]内的日期,和 链接的文字。
asp.net的实现方式如下:
string strHTML = "<li><a href=\http://www.webkaka.com/blog/archives/how-to-add-links-on-baidu-blog.html\ title=\"怎样在百度空间添加友情链接\"><span class=\"article-date\">[14/11]</span>怎样在百度空间添加友情链接</a></li>";
string pattern = "http://([^\\s]+)\".+?span.+?\\[(.+?)\\].+?>(.+?)<";
Regex reg = new Regex( pattern, RegexOptions.IgnoreCase );
MatchCollection mc = reg.Matches( strHTML );
if (mc.Count > 0)
{
foreach (Match m in mc)
{
Console.WriteLine( m.Groups[1].Value );
Console.WriteLine( m.Groups[2].Value );
Console.WriteLine( m.Groups[3].Value );
}
}
2、asp.net正则表达式删除HTML代码
public static string NoHTML(string Htmlstring) //替换HTML标记
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", 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, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}