.net - 抓取标签之间的所有 html

标签 .net regex

经过数小时的搜索和反复试验,似乎无法得到这个。我试图返回两个 html 标签之间的文本。问题是文本跨越多行。这是一个例子。如果有人能找出一个正则表达式来匹配 html 标签之间的所有内容。

<section id="mysection">
The text always starts on the line after the opening section tag.
It can be anything and even span multiple lines.
The closing tag always comes after the last line of text.
</section>

我试过了

Regex.Match(html, "<section id=\"mysection\">/s+(.*?)/s+</section>");

取得了一些成功,但只有在只有一行文本的情况下才有效,而不是在我们有换行符
之类的情况下。使用上面的示例,我希望它匹配“文本总是在开始部分标记之后的行开始。 它可以是任何东西,甚至可以跨越多行。 结束标记始终出现在最后一行文本之后。”

最佳答案

使用这个:

Regex.Match(html, "\\<section id=\"mysection\"\\>(.*?)\\</section\\>", 
            RegexOptions.Singleline);

根据 RegexOptions.Singleline 的文档:

Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

此外,您的尖括号需要转义。

关于.net - 抓取标签之间的所有 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16246896/

相关文章:

asp.net - LabelFor + EditorFor 在同一行?

c# - 什么会导致 throw 重置调用堆栈(我使用的是 "throw",而不是 "throw ex")

c# - 查找字符串中所有单元格地址的正则表达式

c# - 错误继承网页控制

c# - Visual Studio 图标的含义

正则表达式搜索并替换为条件 : works in Notepad++, 不在 Powergrep 中?

arrays - 匹配数组时Powershell使用通配符

php - 错误消息自动隐藏/消失并在POST后停留在输入/表单页面上

javascript - 正则表达式 - 查找未被 [] 包围的特定模式

c# - 无法完成 IDeskBand2 接口(interface)的实现