1

我正在尝试从两行 HTML 中解析文本。

Dim PattStats As New Regex("class=""head"">(.+?)</td>"+ 
                           "\n<td>(.+?)</td>")
Dim makor As MatchCollection = PattStats.Matches(page)

For Each MatchMak As Match In makor
    ListView3.Items.Add(MatchMak.Groups(1).Value)
Next

我添加了\n以匹配下一行,但由于某种原因它不起作用。这是我运行正则表达式的来源。

<table class="table table-striped table-bordered table-condensed">
  <tbody>
    <tr>
      <td class="head">Health Points:</td>
      <td>445 (+85 / per level)</td>
      <td class="head">Health Regen:</td>
      <td>7.25</td>
    </tr>
    <tr>
      <td class="head">Energy:</td>
      <td>200</td>
      <td class="head">Energy Regen:</td>
      <td>50</td>
    </tr>
    <tr>
      <td class="head">Damage:</td>
      <td>53 (+3.2 / per level)</td>
      <td class="head">Attack Speed:</td>
      <td>0.694 (+3.1 / per level)</td>
    </tr>           
    <tr>
      <td class="head">Attack Range:</td>
      <td>125</td>
      <td class="head">Movement Speed:</td>
      <td>325</td>
    </tr>
    <tr>
      <td class="head">Armor:</td>
      <td>16.5 (+3.5 / per level)</td>
      <td class="head">Magic Resistance:</td>
      <td>30 (+1.25 / per level)</td>
    </tr>       
    <tr>
      <td class="head">Influence Points (IP):</td>
      <td>3150</td>
      <td class="head">Riot Points (RP):</td>
      <td>975</td>
    </tr>
  </tbody>
</table>

我想<td class...>在一个正则表达式中匹配第一行和以下行:/

4

1 回答 1

1

描述

此正则表达式将查找td标签并以两个一组的形式返回它们。

<td\b[^>]*>([^<]*)<\/td>[^<]*<td\b[^>]*>([^<]*)<\/td>

在此处输入图像描述

概括

  • <td\b[^>]*>找到第一个 td 标签并使用任何属性
  • ([^<]*)捕获第一个内部文本,这可能是贪婪的,但我们假设单元格没有嵌套标签
  • <\/td>找到关闭标签
  • [^<]*移过所有其余的文本直到你,这假设在第一个和第二个 td 标签之间没有额外的标签
  • <td\b[^>]*>找到第二个 td 标记并使用任何属性
  • ([^<]*)捕获第二个内部文本,这可能是贪婪的,但我们假设单元格没有嵌套标签
  • <\/td>找到关闭标签

团体

第 0 组将获得整个字符串

  1. 将有第一个 td 组
  2. 将有第二个 td 组

VB.NET 代码示例:

Imports System.Text.RegularExpressions
Module Module1
  Sub Main()
    Dim sourcestring as String = "replace with your source string"
    Dim re As Regex = New Regex("<td\b[^>]*>([^<]*)<\/td>[^<]*<td\b[^>]*>([^<]*)<\/td>",RegexOptions.IgnoreCase OR RegexOptions.Singleline)
    Dim mc as MatchCollection = re.Matches(sourcestring)
    Dim mIdx as Integer = 0
    For each m as Match in mc
      For groupIdx As Integer = 0 To m.Groups.Count - 1
        Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
      Next
      mIdx=mIdx+1
    Next
  End Sub
End Module

$matches Array:
(
    [0] => Array
        (
            [0] => <td class="head">Health Points:</td>
          <td>445 (+85 / per level)</td>
            [1] => <td class="head">Health Regen:</td>
          <td>7.25</td>
            [2] => <td class="head">Energy:</td>
          <td>200</td>
            [3] => <td class="head">Energy Regen:</td>
          <td>50</td>
            [4] => <td class="head">Damage:</td>
          <td>53 (+3.2 / per level)</td>
            [5] => <td class="head">Attack Speed:</td>
          <td>0.694 (+3.1 / per level)</td>
            [6] => <td class="head">Attack Range:</td>
          <td>125</td>
            [7] => <td class="head">Movement Speed:</td>
          <td>325</td>
            [8] => <td class="head">Armor:</td>
          <td>16.5 (+3.5 / per level)</td>
            [9] => <td class="head">Magic Resistance:</td>
          <td>30 (+1.25 / per level)</td>
            [10] => <td class="head">Influence Points (IP):</td>
          <td>3150</td>
            [11] => <td class="head">Riot Points (RP):</td>
          <td>975</td>
        )

    [1] => Array
        (
            [0] => Health Points:
            [1] => Health Regen:
            [2] => Energy:
            [3] => Energy Regen:
            [4] => Damage:
            [5] => Attack Speed:
            [6] => Attack Range:
            [7] => Movement Speed:
            [8] => Armor:
            [9] => Magic Resistance:
            [10] => Influence Points (IP):
            [11] => Riot Points (RP):
        )

    [2] => Array
        (
            [0] => 445 (+85 / per level)
            [1] => 7.25
            [2] => 200
            [3] => 50
            [4] => 53 (+3.2 / per level)
            [5] => 0.694 (+3.1 / per level)
            [6] => 125
            [7] => 325
            [8] => 16.5 (+3.5 / per level)
            [9] => 30 (+1.25 / per level)
            [10] => 3150
            [11] => 975
        )

)

免责声明

用正则表达式解析 html 确实不是最好的解决方案,因为有很多我们无法预测的边缘情况。但是,在这种情况下,如果输入字符串始终如此基本,并且您愿意接受正则表达式在 100% 的情况下无法正常工作的风险,那么此解决方案可能对您有用。

于 2013-05-29T12:27:37.620 回答