<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>比如以上html代码片段里,我想取<p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>这段, 其它的不取,请问如何写正则表达式???

解决方案 »

  1.   

    var s='<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>';
    alert(s.replace(/^<p.+?(?=<p)/,''));
      

  2.   

    <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>这段存在很多html代码中,不仅仅是我给出这段代码片段
      

  3.   


    r eg="<p class=\"t\"></p>-<a herf=(?<url>.+?)>(?<content>.+?)</a>";
    我用以上表达式的话无法实现
      

  4.   


    <span>4564373</span ><br/> <span>45673</span ><br/>
    <p class="t">22455</p> - <a herf="xxxxxx.html">xxxxxxxx</a>
    <span>45668973</span ><br/>
    <p class="t">444566</p> - <a herf="xxxxxx.html">xxxxxxxx</a>
    <div><p class="t">46643</p></div>
    <p class="t">111444</p> - <a herf="xxxxxx.html">xxxxxxxx</a>
    <table><tr><td>435678</td></tr></table>以上html也只是一个片段,想说明的是不能用排除替换来做
      

  5.   

    <p class="t">111444</p> - <a herf="xxxxxx.html">xxxxxxxx</a>
    我想要取以上这段,其中数值和xxxx是动态的
      

  6.   

    问题补充:以上给出的html代码片段只是一种类似的构造,真实数据源的是一个网页的内容。 一个网页的html内容有很多不固定的标签,但我只能取类似于:<p class="t"></p> - <a herf="xxxxxx.html">xxxxxxxx</a> 这段,即p标签,class等于t的,同时这段与a标签紧凑在一起的,中间有横线隔着,如上面一 样,横线两头可能有空格,我用以下方法取的不正确:(C#代码)string p = "<p class=\"t\">(?<content1>.+?)</p>\\s[-]\\s<a href=(?<url>.+?)>(? <content2>.+?)</a>"; Regex reg = new Regex(p, RegexOptions.IgnoreCase | RegexOptions.Compiled); MatchCollection ms = reg.Matches(myHtml);foreach (Match m in ms) { string url = m.Groups["url"].Value;string content1 = m.Groups["content1"].Value;string content2 = m.Groups["content2"].Value;}以上代码可以抓取所需要的,但不需要的也抓取了,比如:<p class="t">的也抓取了,我要的 是紧靠着-<a>这个整体的内容。即只想抓取<p class="t">xxxxxx</p> - <a herf="xxxxxx.html">xxxxxxxx</a> 这样的组合内容, 单独的<p class="t">xxxxxx</p> 不抓取!!
      

  7.   

    var s='<span>4564373</span ><br/> <span>45673</span ><br/>\
    \<p class="t">22455</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\
    \<span>45668973</span ><br/>\
    \<p class="t">444566</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\
    \<div><p class="t">46643</p></div>\
    \<p class="t">111444</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\
    \<table><tr><td>435678</td></tr></table>';
     var ar=new Array(),r=null;
     var reg=/<p class="t">\d+<\/p> - <a[^>]+>.+?<\/a>/ig;
    while(r=reg.exec(s)){
            ar.push(r[0]);
    }
    alert(ar);
      

  8.   

    谢谢你的回复,你能否配合我这种代码来写呢:
    string p = "<p class=\"t\">(?<content1>.+?)</p>\\s[-]\\s<a href=(?<url>.+?)>(? <content2>.+?)</a>"; Regex reg = new Regex(p, RegexOptions.IgnoreCase | RegexOptions.Compiled); MatchCollection ms = reg.Matches( myHtml);foreach (Match m in ms) { string url = m.Groups["url"].Value;string content1 = m.Groups["content1"].Value;string content2 = m.Groups["content2"].Value;}因为我想取括号里组合的值,你写的正则我无法套用或应用,不好意思,麻烦你了,(以上是c#代码)
      

  9.   

    <p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>
    string p = "<p class=\"t\">(?<content1>.+?)</p>\\s[-]\\s<a href=(?<url>.+?)>(? <content2>.+?)
    这你能匹配到才见鬼了,当然,也可能是你写例子时手误,那就当我没说var reg = (/<p[^>]*class="t">([^<>]+?)<\/p>[\s]*?\-[\s]*?<a[^>]*href="([^"]*?)">([^<>]*?)<\/a>/i);
      

  10.   


    谢谢你的回答,你给的正则也适应我给出的例子数据,按照你9#的代码,有一小小的变化就是横线前后的空格不固定,有多个,有回车等,是不是要加\s呢??
    另外我想将你的正则尽量往我代码靠,但都没执行成功,可能.NET里的正则与js有所不同吧!
      

  11.   

    是的,空格不固定,就加上 \s
    var reg=/<p class="t">\d+<\/p>\s*-\s*<a[^>]+>.+?<\/a>/ig;
      

  12.   

    该问题最终没有用正则!详见http://bbs.csdn.net/topics/390281495