求一个正则表达式,能够分析html标签的属性
属性出现的可能
<element id=值无引号 无值属性 class="值1 值2" style='bg:#d; w:2; '></element>
有哪位大神能够帮帮忙给一个么~
网上找了一个"([^\s=]+)=([^\s>]+)"但是这个是根据空格来截断的。遇到一个属性有好几个值就不行。没有值的空属性也不行。
属性出现的可能
<element id=值无引号 无值属性 class="值1 值2" style='bg:#d; w:2; '></element>
有哪位大神能够帮帮忙给一个么~
网上找了一个"([^\s=]+)=([^\s>]+)"但是这个是根据空格来截断的。遇到一个属性有好几个值就不行。没有值的空属性也不行。
解决方案 »
- Bitmap.LockBits 方法 (Rectangle, ImageLockMode, PixelFormat)锁定的区域有何用处?
- winform中combobox和datagridview的问题
- asp.net导出的word在打开时遇到文件格式问题 在线等!!
- 高手帮我看看这个类,报死循环异常啊,不知道怎么改.5555
- C#释放Oracle连接
- 怎样将SESSION 保存在SQL数据库中?/?/
- WorkBookSheet问题
- 请教:如何用C#从文件中读取数据
- 想学C#只学了语法,想从案例着手,但发现知识不够
- App.config中 除了connectionStrings和appSettings字节,其它字节的数据怎么读取,更新,删除
- 怎么通过代码能把DEV控件组里的gridview 控件全部展开分组?
- Properties中Settings.settings中范围属性的值 用户和应用程序的区别和存储位置
只要列出属性了
([^\s=]+)=(.*?)(?=id|class|style|>)
<a class='ca cb cc' tag name=abc></a>
这里的tag属性无法获得到。就差这个了
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));
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));//看看
你的截图里的第一个也是不对的
这个分析出来应该是
id 值无引号
无值属性
class 值1 值2
style bg:#d; w:2;