请问一下,贪婪一般都是向后贪婪,比如 <div>1 </div> <div>23 </div> <div>4 </div> 如果我匹配 <div.*?>.*?2.*? <\/div>那么结果将会是 <div>1 </div> <div>23 </div>,请问如何只取到 <div>23 </div> 这个呢,

解决方案 »

  1.   

    $hm = '<div a=1>1 </div> <div>23 </div> <div>4 </div>';preg_match_all('/<div[^>]*>(.*?)<\/div>/i',$hm,$m);
    echo htmlspecialchars($m[0][1]);
      

  2.   

    ls的不行,<div.*?>.*?2.*? <\/div>  这个是指要匹配出含有2的那个div标签,所以匹配要的记过是<div>23 </div>而不是<div>1 </div>
      

  3.   


    $reg = "~<div>1 </div> <div>(.*?)</div> <div>4 </div>~";
    preg_match_all($reg,$source,$m);
    print_r($m);
      

  4.   

    $hm = '<div a=1>1 </div> <div>23 </div> <div>4 </div>';preg_match_all('/<div[^>]*>([^>]*2.[^>]*)<\/div>/i',$hm,$m);
    print_R($m);
      

  5.   

    sorry,应该是
    preg_match_all('/<div[^>]*>([^>]*2[^<]*)<\/div>/i',$hm,$m);
      

  6.   

    <div>(这里再也不能出现<div>这个标签了,其他任何字符都可以.)这里会出现某个特定的字符.如(采集)这里是任意字符.最后跟结束的div标签</div>原始字符串
    <tr> <td style="text-align: center;" bgcolor="#ebe9e4" height="24">12000元 </td> </tr> <tr> <td colspan="4" height="24"> <div style="text-align: right;"> <span style="color: rgb(0, 0, 0);">信息采集;09-11-13 制表: </span> <a target="_blank" href="http://www.xxx.com.cn/"> <span style="color: rgb(0, 0, 0);">某某 </span> </a> <span style="color: rgb(0, 0, 0);">汽车网   </span> <a href="http://www.xxx.com.cn/"> <span style="color: rgb(0, 0, 0);">http://www.xxx.com.cn </span> </a> </div> </td> </tr> <tr> <td style="text-align: center;" bgcolor="#ebe9e4" height="24">某某 </td>
    我想要的匹配结果是<tr> <td colspan="4" height="24"> <div style="text-align: right;"> <span style="color: rgb(0, 0, 0);">信息采集;09-11-13 制表: </span> <a target="_blank" href="http://www.xxx.com.cn/"> <span style="color: rgb(0, 0, 0);">某某 </span> </a> <span style="color: rgb(0, 0, 0);">汽车网   </span> <a href="http://www.xxx.com.cn/"> <span style="color: rgb(0, 0, 0);">http://www.xxx.com.cn </span> </a> </div> </td> </tr>  中间这个<tr>行
      

  7.   

    preg_match_all('/<div[^>]*>([^>]*2[^<]*)<\/div>/iS',$hm,$m);
      

  8.   

    preg_match_all('/<div[^>]*>([^>]*2[^<]*)<\/div>/i',$hm,$m);
    如果用这个正则来匹配我刚发的那个原始字符串,漏洞就是入如果<div>1 </div> <div><b>1</b>23 </div> <div>4 </div> 这样就匹配不到了.多个b标签
      

  9.   

    最重要的想法是<div><div>任意字符</div></div> 在"<div>任意字符</div>" 这个任意字符里一定不能够再次出现<div 这个标签
      

  10.   

    比如[^abcde] 这个正则实际上就是匹配非.但只能针对单个字符.要是我这个单个字符是一个单词呢.就是在这里一定不能出现某个单词如 word 或者中文 "采集" 这样子.把单个字符变成一个单词
      

  11.   

    那你就先匹配<div>内所有的内容,包括html标签,然后逐个strpos判断下匹配的内容里是否包含'2'.
    你这样不断的加规则谁也没办法给你确切的建议,规则跨度太大,你可以仔细审核下匹配的具体的规则,1,2,3,4,5列出来,什么什么情况才匹配..
      

  12.   

    <tr> <td style="text-align: center;" bgcolor="#ebe9e4" height="24">12000元 </td> </tr> <tr> <td colspan="4" height="24"> <div style="text-align: right;"> <span style="color: rgb(0, 0, 0);">信息采集;09-11-13 制表: </span> <a target="_blank" href=" http://www.xxx.com.cn/"> <span style="color: rgb(0, 0, 0);">某某 </span> </a> <span style="color: rgb(0, 0, 0);">汽车网   </span> <a href=" http://www.xxx.com.cn/"> <span style="color: rgb(0, 0, 0);"> http://www.xxx.com.cn </span> </a> </div> </td> </tr> <tr> <td style="text-align: center;" bgcolor="#ebe9e4" height="24">某某 </td>
    这个就是原始字符串.抱歉了
      

  13.   

    只匹配中间那个<tr>到</tr> 包括tr标签.其实就是匹配<tr></tr>之间不能再有<tr class="*"之类的属性>这个标签,而且这个tr标签里一定包含有采集2个汉字
      

  14.   


    <div>(?!<div).*?<\/div>
    楼主知道两个东西就会开朗了.
    1 (?!<div).* 就是这任意字符里一定不能够再次出现 <div
    2 ? 限定为非贪婪,只要碰到第一个<\/div> 匹配完成。