码迷,mamicode.com
首页 > Web开发 > 详细

写个匹配某段html dom代码某属性的正则匹配方法

时间:2014-09-16 15:38:50      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   div   sp   cti   代码   

private static string GetHtmlDomAttr(string html, string id, string attrname)
        {
            string xmatchstring = "<[^<>]+id=\"{0}\"\\s*{1}=\"(?<attvalue>\\S*)\"[^>]*>|<[^<>]+{1}=\"(?<attvalue>\\S*)\"\\s*id=\"{0}\"[^>]*>";
            string xmathcstringend = string.Format(xmatchstring, id, attrname);
            MatchCollection xmatchs = Regex.Matches(html, xmathcstringend, RegexOptions.IgnoreCase);
            if (xmatchs.Count > 0)
            {
                Match fristMatch = xmatchs[0];
                return fristMatch.Groups["attvalue"] == null ? "" : fristMatch.Groups["attvalue"].Value;
            }
            //Match xmatch = Regex.Match(html, xmathcstringend, RegexOptions.IgnoreCase);
            //if (xmatch != null)
            //{
            //    return xmatch.Groups["attvalue"]==null?"":xmatch.Groups["attvalue"].Value;
            //}
            return "";
        }

 

写个匹配某段html dom代码某属性的正则匹配方法

标签:style   blog   color   io   for   div   sp   cti   代码   

原文地址:http://www.cnblogs.com/zihunqingxin/p/3974775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!