废话不多说,直接代码
1、要匹配的html代码
123456
2、正则代码
可以根据id匹配,也可以根据class匹配
string html = “html代码”;//Regex reg = new Regex(@"<(?[\w]+)[^>]*\sclass=(? [""']?)conItem(?(Quote)\k)[""']?[^>]*>((?<\k [^>]*>)| >(?<-Nested>)|.*?)* >", RegexOptions.IgnoreCase | RegexOptions.Singleline); Regex reg = new Regex(@"<(? [\w]+)[^>]*\s[iI][dD]=(? [""']?)footer(?(Quote)\k)[""']?[^>]*>(((?<\k [^>]*>)| >(?<-Nested>)|.*?)*) >", RegexOptions.IgnoreCase | RegexOptions.Singleline); MatchCollection mc = reg.Matches(html); if (mc.Count > 0) { Response.Write(mc.Count.ToString()); foreach (Match m in mc) { string strItem = m.Value; Response.Write(strItem + " \r\n-------------------------\r\n"); } }
代码收集于网络,亲测有效!