如何从 <p> 中提取内容HTML 标签 [英] How to extract content from <p> HTML tag

查看:64
本文介绍了如何从 <p> 中提取内容HTML 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 HTML 作为输入:

I have the following HTML as input:

<p>Hello</p>
<p>How are you?</p>
<div>Hello again</div>

如何只输出Hello"?(仅来自第一个 p 标签的内容).我怎样才能只访问第二个 p-tag 内容?

How can I only output "Hello" from this? (only content from the first p-tag). And how can I also access only the second p-tag content?

所以输出应该是:

string p1 = "Hello"
string p2 = "How are you?"

到目前为止我的代码.完全错误!!!帮助!

My code so far. Full error!!! Help!

using System.Text.RegularExpressions;
string p1 = Regex.Match("<p>(.*?)</p>"[0], myString);
string p2 = Regex.Match("<p>(.*?)</p>"[1], myString);

推荐答案

我想你可能正在寻找这样的东西:

I think you might be looking for something like this:

Regex r = new Regex("<p>(.*?)<\\/p>");
string p1 = r.Matches(myString)[0].Groups[1].Value;
string p2 = r.Matches(myString)[1].Groups[1].Value;

输出如下:

Hello
How are you?

请记住,虽然这不是最安全的方法,但迭代结果可能有助于记住:

Keep in mind though this isn't the most bombproof method, iterating through the results might be useful to keep in mind going forward:

foreach (Match m in r.Matches(myString))
{
    Console.WriteLine(m.Groups[1].Value);
}

这篇关于如何从 &lt;p&gt; 中提取内容HTML 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆