0

在 C# 中,我的正则表达式具有以下模式:

string pattern = "<div class=\"alt\" title=\"[\\w\\s]+\"><strong>([\\w\\s]+)</strong></div>";

我像这样创建一个Match对象:

status = Regex.Match(html, pattern);

但是,如果我在状态上调用 .groups() ,我会得到空白文本,即使有匹配项。我是否正确提取了组?

编辑:这是一些 HTML,

          <tr>
            <td>
                    <div class="alt" title="Released to Manufacturing">
                            <strong>Released to Manufacturing</strong>
4

2 回答 2

0
string strRegex = @"<div class=""alt"" title=""[\w\s]+""><strong>([\w\s]+)</strong></div>";
RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"<div class=""alt"" title=""released""><strong>Released</strong></div>";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
    if (myMatch.Success)
    {
        var value = myMatch.Groups[1].Value;
    }
}

使用RegexHero验证

于 2013-10-23T06:39:31.657 回答
0

正则表达式不用于解析 html..

使用像Htmlagilitypack这样的 html 解析器

   HtmlDocument doc = new HtmlDocument();
   doc.Load(yourStream);
   var altElementValues= doc.DocumentNode
                            .SelectNodes("//div[@class='alt']/strong")
                            .Select(x=>x.InnerText);
于 2013-10-23T06:41:22.877 回答