Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在行之间获取字符串<;tr>;从以某个单词开头,以某个单词结尾的Html页面_C#_Html_Regex - Fatal编程技术网

C# 如何在行之间获取字符串<;tr>;从以某个单词开头,以某个单词结尾的Html页面

C# 如何在行之间获取字符串<;tr>;从以某个单词开头,以某个单词结尾的Html页面,c#,html,regex,C#,Html,Regex,我有一个HTML页面,它只有一个标记,但有许多和标记 例如: 名称1 一些文本 一些文本 1. 一些文本 -------- 一些文本 一些文本 全部的 -------- 1989 一些文本 ------------------------------------------------------------------------------ 姓名2 一些文本 一些文本 一些文本 -------- 一些文本 一些文本 一些文本 -------- 一些文本 一些文本 全部的 --------

我有一个HTML页面,它只有一个
标记,但有许多
标记

例如:


名称1
一些文本
一些文本
1.
一些文本
--------
一些文本
一些文本
全部的
--------
1989
一些文本
------------------------------------------------------------------------------
姓名2
一些文本
一些文本
一些文本
--------
一些文本
一些文本
一些文本
--------
一些文本
一些文本
全部的
--------
1979
一些文本
------------------------------------------------------------------------------
名字3
一些文本
一些文本
2.
一些文本
--------
一些文本
一些文本
全部的
--------
1089
一些文本
现在假设我想要NAME1和以下TOTAL之间的行,以及NAME3和以下TOTAL之间的行

此之间可以有任意数量的行和列

行和列的大小不是固定的


因此,输出应该包括
1.
2.

如果您想让组将文本和html分开,请使用以下组:

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

名称(1 | 3)(\s*([^我同意其他人的说法,他们说你应该使用解析器。这种解决方案比正则表达式更健壮。但是,如果你知道运行正则表达式所针对的HTML不会有太大变化,正则表达式方法可以工作。要知道,即使对HTML进行一点小的更改,也可能导致此解决方案在以后失败。例如,如果你向任何在内部行中,这个正则表达式将找不到匹配项。在这种情况下,正则表达式也可以工作,但它会变得更复杂,更难阅读

此正则表达式适用于您在问题中提供的示例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>
]+>\s*名称(?:1 | 3)(?:\s*[\w\s-]+)+\s*(?:\s*(?:\s*[\w\s-]+)+\s*)+?)\s*\s*总计(?:\s*[\w\s-]+)\s**
下面是正则表达式的大致分类:

#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.
#匹配第一行。
]+>#匹配开始TR标记,允许在标记内找到任何属性。
\s*Name(?:1 | 3)#匹配第一个单元格。仅允许其内容为“Name1”或“Name3”。
(?:\s*[\w\s-]+)+#匹配此行中的所有其他单元格。
\s*#匹配行的末尾。
#匹配第一行和最后一行之间的所有行。
(?:
\s*#匹配内部行的开头。
(?:\s*[\w\s-]+)+#匹配当前行中的所有单元格。
\s*#匹配当前行的末尾。
)+?
#匹配最后一行。
\s*#匹配最后一行的开头。
\s*Total#匹配第一个单元格。仅允许其内容为“Total”。
(?:\s*[\w\s-]+)#匹配此行中的所有其他单元格。
+\s*#匹配最后一行的末尾。

我不想使用第三方工具。那么读一下@I4V-也许你应该读一下:@I4V-我会选择HTML解析器,因为我喜欢和家人呆在一起。也就是说,你不能这样做的“鹦鹉学舌”事实是完全错误的(特别是考虑到.NET正则表达式)。