C#使用正则表达式检查文本中的html标记

本文关键字:html 标记 文本 检查 正则表达式 | 更新日期: 2023-09-27 18:27:47

这是我的html:

<div class="bla">
followers
<a href="">followers count </a>
</div>

我想要的是,如果文本中的<div class="bla" />等于followers,那么在文本中获得下一个标签(followers计数)。我知道使用htmlagilitypack会很容易,但在这种情况下不能使用它。如何使用regex?

C#使用正则表达式检查文本中的html标记

这就是您需要的吗?

    var mtch = Regex.Match(input, 
"<div class[ ]?=[ ]?'"bla[^'"]*'"[ ]?>(?<groupName>[''w''W]+</div>");
    if (mtch.Groups["groupName"].Value.ToLower().Contains("followers"))
    {
        //do your stuff
    }