【问题标题】:Regex Matches only getting count 1 instead of 2正则表达式匹配仅获得计数 1 而不是 2
【发布时间】:2013-06-06 10:36:36
【问题描述】:

这是我的正则表达式:

private static Regex _errorRegex = new Regex(" <div class=\"styleRed\">(?<message>.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);

网页有如下数据:

<div class="styleRed">• Zero is required in the Consideration Field for Deed Type CEDOT (Row 1). </div><div class="styleRed">• Zero is required in the Assume/Value Field for Deed Type CEDOT (Row 1). </div>


var matches = _errorRegex.Matches(webpage);

匹配数始终为 1。知道我错过了什么吗?

【问题讨论】:

  • 这需要链接到我最喜欢的 SO 答案 stackoverflow.com/a/1732454/860585
  • 你想做什么?
  • 你能发布足够多的网页以显示它应该匹配两次吗?
  • @dlf:基本上我试图在所有出现的情况下找到
    blah blah
    内的所有文本。
  • 你看过它实际匹配的是什么吗? .* 可能是贪婪的,并且匹配所有内容,直到最后一个 &lt;/div&gt;

标签: c# .net regex c#-4.0


【解决方案1】:

您的正则表达式模式以空格开头。

在您的示例中,HTML 页面上的第二个 div 前面没有空格。

因此,它不匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 2015-08-05
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多