首页 > Web开发 > 详细

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

时间:2014-09-16 15:38:50      阅读:595      评论:0      收藏:0      [点我收藏+]
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代码某属性的正则匹配方法

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!