.net正則マッチングaラベル

1470 ワード





2009-08-26 16:00:51|    : Asp.Net |     
//c#      html  a      
public string matchA(string teststr)
    {
        StringBuilder sb = new StringBuilder();
        //              
        Regex reg = new Regex(@"<\s*a\shref=*[^>]*>([^<]|<(?!/a))*<\s*/a\s*>");
         MatchCollection match = reg.Matches(teststr);
         foreach (Match var in match)
         {
             if (var != null)
             {
                 sb.Append(Server.HtmlEncode(var.Value));
                 sb.Append("<br />");
             }
         }
         return sb.ToString();
    }

//              a      
public void test()
{
        string temp=string.Empty;
        WebRequest webReq = WebRequest.Create(" http://www.163.com");
        WebResponse webRes = webReq.GetResponse();
        Stream resStream = webRes.GetResponseStream();
        StreamReader sr = new StreamReader(resStream, Encoding.Default);
        StringBuilder sb = new StringBuilder();
        while ((temp = sr.ReadLine()) != null)
        {
            sb.Append(temp);
        }
        webRes.Close();
        sr.Close();
       //         
       Response.Write(matchA(sb.ToString()));
   
}