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

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 "";
        }

 

posted @ 2014-09-16 14:00  cclient  阅读(708)  评论(0编辑  收藏  举报