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

解决方案 »

  1.   


    <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;" />最弄不懂的就是正则了,帮我一下吧
      

  2.   

    http://www.oschina.net/project/tag/236/html-parser?lang=20&sort=view
      

  3.   

    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.   

    js
     document.getElenmentsByTargName('p');
     document.getElenmentsByTargName('img')[0];
      

  5.   

    问题我就不回答了
    给点正则的学习资料
    http://topic.csdn.net/u/20120228/11/88a6fd52-de02-4c3e-9f97-a1ffe2fbab24.html