简体   繁体   English

如何使用RegEx Asp.net C#抓取BODY html标签内的所有内容(来自字符串)

[英]How do i grab everything inside the BODY html tag (From a string) using RegEx Asp.net C#

{Yup, the above more or less explains it} :) {是的,上面或多或少解释了它} :)

Regex oRegex = new Regex("<body.*?>(.*?)</body>", RegexOptions.Multiline);

The above doesnt seem to work if the body has any attributes in it. 如果身体中有任何属性,上面似乎不起作用。

With the HTML Agility Pack (assuming it is html, not xhtml): 使用HTML Agility Pack (假设它是html,而不是xhtml):

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
string body = doc.DocumentNode.SelectSingleNode("/html/body").InnerHtml;

Don't use a regular expression. 不要使用正则表达式。 Use something that's meant to parse XML/HTML: 使用旨在解析XML / HTML的东西:

XmlDocument.SelectSingleNode("//body").InnerXml;

Load your string into an XmlDocument , use the SelectSingleNode function (which takes an XPath expression as a parameter), then extract what you need from the resulting XmlNode . 将字符串加载到XmlDocument中 ,使用SelectSingleNode函数(将XPath表达式作为参数),然后从生成的XmlNode中提取所需内容。

我最终通过使用RegexOptions.Singleline而不是使用RegexOptions.Multiline解决了它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Regex的Asp.Net C#HTML字符串预览 - Asp.Net C# Html String Preview using Regex 如何通过 ASP 按钮从 Gridview 中的特定单元格中获取值? ASP.NET C# - How do I grab values from specific cells in a Gridview via ASP Button ? ASP.NET C# C#-如何在ASP.NET MVC的HTML表单中传递内部带有其他模型的模型 - C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVC 我如何使用C#中的regEx从此字符串中获取值来解释regex? - How can I grab a value from this string using regEx in c# explain the regex? 从html解析的正则表达式,如何获取特定的字符串? - Regex from a html parsing, how do I grab a specific string? 如何从Html.TextBox MVC 2 asp.net C#检索值? - How do I retrieve values from Html.TextBox MVC 2 asp.net C#? 如何使用asp.net C#将HTML格式的电子邮件正文传递给javascript函数 - How to pass HTML formatted email body to a javascript function using asp.net c# Html,C#,ASP.NET如何重复包含标签的div标签中的内容来显示数据库中的所有记录? - Html, C#, ASP.NET How can i repeat the contents in div tag containing labels to display all records from database? ASP.Net MVC - 如何在html标签内显示视图模型中的字符串? - ASP.Net MVC - How to display string from view model inside html tag? 在ASP.NET中的JavaScript中使用C#字符串属性 - Using c# string property inside a javascript in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM