
C# 正则表达式提取html中的文本
是这样的,我想提取在一个网页的所有的html代码中提取特定的html代码中的文本。比如说下面的这个:<h1id="artibodyTitle"pid="1"tid="1"...
是这样的,我想提取在一个网页的所有的html代码中提取特定的html代码中的文本。比如说下面的这个:
<h1 id="artibodyTitle" pid="1" tid="1" did="25820705" fid="1666">value</h1>
想提取到这两个标记中的value部分的具体字符串,然后赋值给string型的s1变量。
然后提取下面的这个:
<div class="blkContainerSblkCon BSHARE_POP" id="artibody">
<!-- 视频小点大播放器_正文上方显示 -->
<!-- publish_helper name='原始正文' p_id='1' t_id='1' d_id='25820705' f_id='3' -->
<p>text1</p>
<p>text2</p>
<p>text3</p>
<p>text4</p>
<p>text5</p>
<p align="right">text6</p>
<!-- publish_helper_end -->
</div>
把上面<div class="blkContainerSblkCon BSHARE_POP" id="artibody">和</div>
中间的部分取出来给string型的s2变量。
然后把<div class="blkContainerSblkCon BSHARE_POP" id="artibody">和</div>中间的,去掉<p></p>的部分,这样只有文本的部分给string型的s3变量。
分别写出三个的正则表达式和实现的具体的c#代码。
谢谢各位大神了。
先给这么多分数,答得好了再追加。谢谢了。 展开
<h1 id="artibodyTitle" pid="1" tid="1" did="25820705" fid="1666">value</h1>
想提取到这两个标记中的value部分的具体字符串,然后赋值给string型的s1变量。
然后提取下面的这个:
<div class="blkContainerSblkCon BSHARE_POP" id="artibody">
<!-- 视频小点大播放器_正文上方显示 -->
<!-- publish_helper name='原始正文' p_id='1' t_id='1' d_id='25820705' f_id='3' -->
<p>text1</p>
<p>text2</p>
<p>text3</p>
<p>text4</p>
<p>text5</p>
<p align="right">text6</p>
<!-- publish_helper_end -->
</div>
把上面<div class="blkContainerSblkCon BSHARE_POP" id="artibody">和</div>
中间的部分取出来给string型的s2变量。
然后把<div class="blkContainerSblkCon BSHARE_POP" id="artibody">和</div>中间的,去掉<p></p>的部分,这样只有文本的部分给string型的s3变量。
分别写出三个的正则表达式和实现的具体的c#代码。
谢谢各位大神了。
先给这么多分数,答得好了再追加。谢谢了。 展开
6个回答
展开全部
StringBuilder sb = new StringBuilder();
string re1;
string re2;
re1 = "(?i)<div.+?id=\"artibody\">(.+?)</div>";
re2 = "(?i)<p>(.+?)</p>";
string s2 = Regex.Match(this.textBox1.Text, re1).Groups[1].Value;
MatchCollection m = Regex.Matches(s2, re2);
foreach (Match item in m)
{
sb.Append(item.Groups[1].Value + "\r\n");
}
string s3 = sb.ToString();
string re1;
string re2;
re1 = "(?i)<div.+?id=\"artibody\">(.+?)</div>";
re2 = "(?i)<p>(.+?)</p>";
string s2 = Regex.Match(this.textBox1.Text, re1).Groups[1].Value;
MatchCollection m = Regex.Matches(s2, re2);
foreach (Match item in m)
{
sb.Append(item.Groups[1].Value + "\r\n");
}
string s3 = sb.ToString();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询