[英]c# Regex inside some html tag
I'm trying during some hour with regex to take text inside some html tag: 我正在尝试使用正则表达式在一些小时内将文本置于某个html标记内:
<div class="ewok-rater-header-section">
<ul class="header">
<li><h1>meow</h1></li>
<li><h1>meow2</h1></li>
<li><h1>Time = <span class="work-weight">9.0 minutes</span></h1></li>
</ul>
</div>
i take meow
with 我
meow
var regexpost = new System.Text.RegularExpressions.Regex(@"<h1(.*?)>(.*?)</h1>");
var mpost = regexpost.Match(reqpost);
string lechat = (mpost.Groups[2].Value).ToString();
but not other I like to add meow
in a textbox
, meow2
in a second textbox and 9.0 (minutes) in a last one 但不是其他我喜欢在
textbox
添加meow
,在第二个文本框中添加meow2
,在最后一个文本框中添加9.0(分钟)
In these situations a Html parser can help a lot, and can also be a lot more precise and robust 在这些情况下,Html解析器可以提供很多帮助,也可以更加精确和健壮
Example 例
var html = @"<div class=""ewok-rater-header-section"">
<li><h1>meow</h1></li>
<li><h1>meow2</h1></li>
<li><h1>Time = <span class=""work-weight"">9.0 minutes</span></h1></li>
</div>";
var doc = new HtmlDocument();
doc.LoadHtml(html);
// you can search for the heading
foreach (var node in doc.DocumentNode.SelectNodes("//li//h1"))
{
Console.WriteLine("Found heading : " + node.InnerText);
}
// or you can be more specific
var someSpan = doc.DocumentNode
.SelectNodes("//span[@class='work-weight']")
.FirstOrDefault();
Console.WriteLine("Found span : " + someSpan.InnerText);
Output 产量
Found heading : meow
Found heading : meow2
Found heading : Time = 9.0 minutes
Found span : 9.0 minutes
it s for parse http reponse. 这是解析http响应。 Then is it not slow to use a html parser to create document ?
那么使用html解析器创建文档是不是很慢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.