从类文件中读取.net中的内联样式

时间:2015-03-25 06:11:29

标签: html .net regex

我需要从.net类文件中的html元素中获取样式。

<img alt="" style="width: 81px; height: 61px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: inherit; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px;" src=""https://www.google.co.in/images/srpr/logo11w.png"" />

输出应该是

  

style =“width:81px; height:61px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:inherit; margin-top:0px; margin-right:10px; margin-bottom:0px; margin-left:0px;“

如果通过正则表达式找到它会更好。

2 个答案:

答案 0 :(得分:0)

尝试使用此正则表达式: style="[^"]+"

<强> Demo

答案 1 :(得分:0)

您可以使用style=".*?"正则表达式。

.*?会不合理地匹配任何字符(因此,不会吃掉&#39; src&#39;属性)。

C#代码:

 var input = "<img alt=\"\" style=\"width: 81px; height: 61px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: inherit; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px;\" src=\"\"https://www.google.co.in/images/srpr/logo11w.png\"\" />";
 var MyRegex = new Regex(@"style="".*?""");
 var result = MyRegex.Match(input).Value;
相关问题