当前位置:编程学习 > C#/ASP.NET >>

求代码,将一段HTML中所有P标签中的文字取出,以及第一个img标签内容取出。

求代码,将一段HTML中所有P标签中的文字取出,以及第一个img标签内容取出。 --------------------编程问答-------------------- 用正则

给点HTML源码 --------------------编程问答--------------------
引用 1 楼  的回复:
用正则

给点HTML源码


<h1>标题1</h1>
<p>段落1</p>
<img alt="" src="http://a.cksource.com/c/1/inc/img/demo-little-red.jpg" style="margin-left: 10px; margin-right: 10px; float: left; width: 120px; height: 168px;" />
<p>段落2</p>
<img src="xxxxxxxxxxx" />

想得到的结果是 
字符串text_p保存
<p>段落1</p><p>段落2</p>

字符串text_img保存
<img alt="" src="http://a.cksource.com/c/1/inc/img/demo-little-red.jpg" style="margin-left: 10px; margin-right: 10px; float: left; width: 120px; height: 168px;" />


最弄不懂的就是正则了,帮我一下吧 --------------------编程问答-------------------- http://www.oschina.net/project/tag/236/html-parser?lang=20&sort=view --------------------编程问答--------------------
string tempStr = File.ReadAllText(@"C:\Users\M\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取文档
            string text_p = string.Join("",Regex.Matches(tempStr,@"(?i)<p[^>]*?>[^<>]*?</p>").Cast<Match>().Select(a=>a.Value));
            //<p>段落1</p><p>段落2</p>
            string text_img = Regex.Match(tempStr,@"(?i)<img[^>]*?>").Value;
            //<img alt=\"\" src=\"http://a.cksource.com/c/1/inc/img/demo-little-red.jpg\" style=\"margin-left: 10px; margin-right: 10px; float: left; width: 120px; height: 168px;\" />
--------------------编程问答--------------------
引用 4 楼  的回复:
C# code
string tempStr = File.ReadAllText(@"C:\Users\M\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取文档
            string text_p = string.Join("",Regex.Matches(tempStr,@"(?i)<p[^>]*?>[^<>]……

正则需要怎么去学习啊???感觉正则这个东西比较难。。。额 --------------------编程问答--------------------
引用 5 楼  的回复:
引用 4 楼 的回复:

C# code
string tempStr = File.ReadAllText(@"C:\Users\M\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取文档
string text_p = string.Join("",Regex.Matches(tempStr,@"(?i)<p[^>]*?>[^……

不难,只要你有兴趣。没兴趣的话,就不要想了。 --------------------编程问答-------------------- js
 document.getElenmentsByTargName('p');
 document.getElenmentsByTargName('img')[0];
--------------------编程问答--------------------
引用 5 楼  的回复:
引用 4 楼 的回复:

C# code
string tempStr = File.ReadAllText(@"C:\Users\M\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取文档
string text_p = string.Join("",Regex.Matches(tempStr,@"(?i)<p[^>]*?>[^……

入门还是比较简单的,不过正则和其他的有点区别,需要去记。
比如每个符号代表的意思,必须记下来。正则也就是一些符号的组合! --------------------编程问答--------------------
引用 2 楼  的回复:
引用 1 楼  的回复:

用正则

给点HTML源码

HTML code

<h1>标题1</h1>
<p>段落1</p>
<img alt="" src="http://a.cksource.com/c/1/inc/img/demo-little-red.jpg" style="margin-left: 10px; margin-right: 10px; float: lef……

问题我就不回答了
给点正则的学习资料
http://topic.csdn.net/u/20120228/11/88a6fd52-de02-4c3e-9f97-a1ffe2fbab24.html
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,