3

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

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

4

1 回答 1

1

用这个:

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

根据 RegexOptions.Singleline 的文档:

指定单行模式。更改点 (.) 的含义,使其匹配每个字符(而不是除 \n 之外的每个字符)。

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

于 2013-04-27T01:03:05.077 回答