gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:52:30 25 4
gpt4 key购买 nike

经过数小时的搜索和反复试验,似乎无法得到这个。我试图返回两个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com