写个匹配某段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 "";
        }
原文地址:https://www.cnblogs.com/zihunqingxin/p/3974775.html