如何获得行之间的字符串从Html页面,以某个单词开始,以某个单词结束

本文关键字:单词 页面 结束 开始 Html 之间 何获得 字符串 tr | 更新日期: 2023-09-27 18:15:48

我有一个HTML页面,只有一个<table>标签,但有许多<tr><td>标签。

的例子:

<tr attributes >
    <td>Name1</td>
    <td>some text</td>
    <td>some text</td>
</tr>                                                            1.
<tr>
    <td>some text</td>
    <td>--------</td>
    <td>some text</td>
    <td>some text</td>
</tr>
<tr>
    <td>Total</td>
    <td>--------</td>
    <td>1989</td>
    <td>some text</td>
</tr>
------------------------------------------------------------------------------
<tr attributes >
    <td>Name2</td>
    <td>some text</td>
    <td>some text</td>
</tr>
<tr>
    <td>some text</td>
    <td>--------</td>
    <td>some text</td>
    <td>some text</td>                                            
</tr>
<tr>
    <td>some text</td>
    <td>--------</td>
    <td>some text</td>
    <td>some text</td>
</tr>
<tr>
    <td>Total</td>
    <td>--------</td>
    <td>1979</td>
    <td>some text</td>
</tr>
------------------------------------------------------------------------------
<tr attributes >
    <td>Name3</td>
    <td>some text</td>
    <td>some text</td>
</tr>                                                                  2.
<tr>
    <td>some text</td>
    <td>--------</td>
    <td>some text</td>
    <td>some text</td>
</tr>
<tr>
    <td>Total</td>
    <td>--------</td>
    <td>1089</td>
    <td>some text</td>
</tr>

现在假设我想要NAME1和下面的TOTAL NAME3和下面的TOTAL

在这之间可以有任意数量的行和列。

行和列的大小不是固定的。

所以输出应该包括1.2.

如何获得行之间的字符串<tr>从Html页面,以某个单词开始,以某个单词结束

如果你想用组把文本和html分开使用这个:

<td>Name(1|3)</td>(('s*<td>([^<]+)</td>'s*)+</tr>(.*?)<tr>)+?'s*<td>Total</td>

你必须添加选项"s"(点all模式)

我同意其他人说应该使用解析器的说法。该解决方案将比正则表达式更健壮。但是,如果您知道将运行正则表达式的HTML不会发生太大变化,则可以使用正则表达式方法。要知道,即使对HTML进行很小的更改,也可能导致该解决方案在以后失败。例如,如果向任何内行添加属性,则此正则表达式将找不到匹配项。regex也可以在这种情况下工作,但这会变得更复杂,更难阅读。

这个正则表达式适用于您在问题中提供的示例HTML。使用捕获组1只获取内部行

<tr's+[^>]+>'s*<td>Name(?:1|3)</td>(?:'s*<td>['w's-]+</td>)+'s*</tr>((?:'s*<tr>(?:'s*<td>['w's-]+</td>)+'s*</tr>)+?)'s*<tr>'s*<td>Total</td>(?:'s*<td>['w's-]+</td>)+'s*</tr>

下面是regex的大致分解:

#Matche the first row.
<tr's+[^>]+>                    #Match the opening TR tag, allow for any attributes found inside the tag.
's*<td>Name(?:1|3)</td>         #Match the first cell. Only allow its contents to be "Name1" or "Name3".
(?:'s*<td>['w's-]+</td>)+       #Match all other cells in this row.
's*</tr>                        #Match the end of the row.
#Match all rows between the first and last row.
(?:
    's*<tr>                         #Match the beginning of an inner row.
        (?:'s*<td>['w's-]+</td>)+   #Match all the cells in the current row.
    's*</tr>                        #Match the end of the current row.
)+?
#Match the last row.
's*<tr>                         #Match the beginning of the last row.
's*<td>Total</td>               #Match the first cell. Only allow its contents to be "Total".
(?:'s*<td>['w's-]+</td>)        #Match all other cells in this row.
+'s*</tr>                       #Match the end of the last row.