首页  ·  知识 ·  云计算
正则表达式提取网址、标题、图片等一例
佚名  http://www.zu14.cn/2008/11/05/net_mail/  综合  编辑:dezai  图片来源:网络
在一些抓取、过滤等情况下, 正则表达式 regular expression 的优势是很明显的。 例如,有如下的字符串: "

在一些抓取、过滤等情况下, 正则表达式 regular expression 的优势是很明显的。

例如,有如下的字符串:

  • http://www.abcxyz.com/something/article/143.htm" title="FCKEditor高亮代码插件测试">FCKEditor高亮代码插件测试
  • 在,需要提取 href 后面的网址,[]内的日期,和 链接的文字。

    下面给出C#, ASP 和 javascript 的实现方式

     C#的实现

    string strHTML = "

  • [09/11]FCKEditor高亮代码插件测试
  • ";
    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 );
        }
    }

    ASP的实现

    <%
     Dim str, reg, objMatches
     str = "

  • http://localhost/Z-Blog18/article/143.htm"" title=""FCKEditor高亮代码插件测试"">[09/11]FCKEditor高亮代码插件测试
  • "

     Set reg = new RegExp

     reg.IgnoreCase = True
     reg.Global = True

     reg.Pattern = "http://([^\s]+)"".+?span.+?\[(.+?)\].+?>(.+?)<"

     Set objMatches = reg.Execute(str)

     If objMatches.Count > 0 Then
      Response.Write("网址:")
      Response.Write(objMatches(0).SubMatches(0))
      Response.Write("
    ")
      Response.Write("日期:")

      Response.Write(objMatches(0).SubMatches(1))

      Response.Write("
    ")

      Response.Write("标题:")
      Response.Write(objMatches(0).SubMatches(2))
     End If
    %>
     javascript的实现

     

    本文作者:佚名 来源:http://www.zu14.cn/2008/11/05/net_mail/
    CIO之家 www.ciozj.com 微信公众号:imciow
       
    免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
    延伸阅读