如何获得行之间的字符串从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.
如果你想用组把文本和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.
相关文章:
- 如何使WP8.1应用程序在启动时从不同的页面启动
- 获取以@开头的单词,例如正则表达式和javascript提到的Facebook
- 我想在新页面/窗口中显示 PDF
- Facebook SDK:作为页面 ID 发布到页面
- 如何使用 Windows Phone 应用程序检测浏览器中页面的加载
- 确定用户如何到达 403 页面
- 在页面视图上设置会话变量
- 如何在 C# asp.net 中的另一个页面中更改站点地图中 URL 的默认重定向页面
- 从 c# 后面的代码编写页面加载 javascript 函数
- MVC 5 重定向至“登录页面与 OWIN 不起作用”诊断树
- 具有 2 个相互链接的选项卡和页面泛型类型的选项卡控件类
- 在 ASP.NET 如何设置需要授权的页面
- 如何检查 PDF 页面的嵌入图像的分辨率 (DPI)
- 如何在代码隐藏中创建页面,并将主页导航到 Windows 应用商店应用中创建的页面
- 在foreach(单词中的字符串单词)中,如果(if->true)C#,我怎么能转到下一个单词
- 为什么此页面保持为零?这是一个范围/托管/参考/等问题吗?
- 如何在特定框架中加载页面
- 单击链接时,在标准浏览器中打开新页面
- 如何删除页面顶部不包含某个单词的巨大PDF中的页面?希望在C#中
- 如何获得行之间的字符串
从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.
如果你想用组把文本和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.
相关文章:
- 如何使WP8.1应用程序在启动时从不同的页面启动
- 获取以@开头的单词,例如正则表达式和javascript提到的Facebook
- 我想在新页面/窗口中显示 PDF
- Facebook SDK:作为页面 ID 发布到页面
- 如何使用 Windows Phone 应用程序检测浏览器中页面的加载
- 确定用户如何到达 403 页面
- 在页面视图上设置会话变量
- 如何在 C# asp.net 中的另一个页面中更改站点地图中 URL 的默认重定向页面
- 从 c# 后面的代码编写页面加载 javascript 函数
- MVC 5 重定向至“登录页面与 OWIN 不起作用”诊断树
- 具有 2 个相互链接的选项卡和页面泛型类型的选项卡控件类
- 在 ASP.NET 如何设置需要授权的页面
- 如何检查 PDF 页面的嵌入图像的分辨率 (DPI)
- 如何在代码隐藏中创建页面,并将主页导航到 Windows 应用商店应用中创建的页面
- 在foreach(单词中的字符串单词)中,如果(if->true)C#,我怎么能转到下一个单词
- 为什么此页面保持为零?这是一个范围/托管/参考/等问题吗?
- 如何在特定框架中加载页面
- 单击链接时,在标准浏览器中打开新页面
- 如何删除页面顶部不包含某个单词的巨大PDF中的页面?希望在C#中
- 如何获得行之间的字符串
从Html页面,以某个单词开始,以某个单词结束