求一个正则表达式,能够分析html标签的属性
属性出现的可能
<element id=值无引号 无值属性 class="值1 值2" style='bg:#d; w:2; '></element>
有哪位大神能够帮帮忙给一个么~
网上找了一个"([^\s=]+)=([^\s>]+)"但是这个是根据空格来截断的。遇到一个属性有好几个值就不行。没有值的空属性也不行。

解决方案 »

  1.   

    这个真不好写,
    只要列出属性了
    ([^\s=]+)=(.*?)(?=id|class|style|>)
      

  2.   

    ([^\s=]+)=(['"\s]?)([^'"]+)\2(?=\s|$|>)
      

  3.   

    这个省去了2次判定。但是和2次判断一样。没有值的属性也无法获取
    <a class='ca cb cc' tag  name=abc></a>
    这里的tag属性无法获得到。就差这个了
      

  4.   

    可以的啊            string input = @"<element id=值无引号 无值属性 class=""值1 值2"" style='bg:#d; w:2; '></element>";
                Dictionary<string,string> dic = new Dictionary<string,string>();
                MatchCollection mc = Regex.Matches(input, @"([^\s=]+)=(['""\s]?)([^'""]+)\2(?=\s|$|>)");
                foreach (Match m in mc)
                {
                    dic.Add(m.Groups[1].Value, m.Groups[3].Value);
                }
                dic.ToList().ForEach(e => Console.WriteLine(e.Key+"\t"+e.Value));
      

  5.   


       string input = @"<element id=值无引号 无值属性 class=""值1 值2"" style='bg:#d; w:2; '></element>";
                Dictionary<string,string> dic = new Dictionary<string,string>();
                MatchCollection mc = Regex.Matches(input, @"([^\s=]+)=(['""\s]?)([^'""]+)\2(?=\s|$|>)");
                foreach (Match m in mc)
                {
                    dic.Add(m.Groups[1].Value, m.Groups[3].Value);
                }
                dic.ToList().ForEach(e => Response.Write(e.Key+"\t"+e.Value));//看看
      

  6.   

    你看我的截图分析的那个form
    你的截图里的第一个也是不对的
    这个分析出来应该是
    id                 值无引号
    无值属性 
    class              值1 值2
    style              bg:#d; w:2;