日期:2014-05-18  浏览次数:21159 次

求C#正则获得指定标签的内容
HTML code
<li class="h4">x1x</li><li class="h4">x2x</li><li class="h4">xfx</li><li class="h4">xsx</li>


如上的html代码,具有多个标签,想获得每个]<li class="h4">想要获得内容</li>.
谢谢大家.对于正则是新人.请多帮忙.

------解决方案--------------------

C# code

string htmlStr = @"<li class=""h4"">x1x</li><li class=""h4"">x2x</li><li class=""h4"">xfx</li><li class=""h4"">xsx</li>";


Regex re = new Regex(@"(?<=<li\s*class=""h4"">)[^<]+(?=</li>)", RegexOptions.None);
MatchCollection mc = re.Matches("text");
foreach (Match ma in mc)
{
  //ma.Value就是你要值
}

------解决方案--------------------
C# code

var reg = new Regex("<li[^>]*class=(['\"])h4\\1[^>]*>(?<content>.*?)</li>", RegexOptions.IgnoreCase);
            const string str =
                "<li class=\"h4\">x1x</li><li class=\"h4\">x2x</li><li class=\"h4\">xfx</li><li class=\"h4\">xsx</li>";
            var ms = reg.Matches(str);
            foreach (Match match in ms)
            {
                Console.WriteLine(match.Groups["content"].Value);
            }