正则高手来解决一下(50)分.在线等 请问一下,贪婪一般都是向后贪婪,比如 <div>1 </div> <div>23 </div> <div>4 </div> 如果我匹配 <div.*?>.*?2.*? <\/div>那么结果将会是 <div>1 </div> <div>23 </div>,请问如何只取到 <div>23 </div> 这个呢, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $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]); ls的不行,<div.*?>.*?2.*? <\/div> 这个是指要匹配出含有2的那个div标签,所以匹配要的记过是<div>23 </div>而不是<div>1 </div> $reg = "~<div>1 </div> <div>(.*?)</div> <div>4 </div>~";preg_match_all($reg,$source,$m);print_r($m); $hm = '<div a=1>1 </div> <div>23 </div> <div>4 </div>';preg_match_all('/<div[^>]*>([^>]*2.[^>]*)<\/div>/i',$hm,$m);print_R($m); sorry,应该是preg_match_all('/<div[^>]*>([^>]*2[^<]*)<\/div>/i',$hm,$m); <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>行 preg_match_all('/<div[^>]*>([^>]*2[^<]*)<\/div>/iS',$hm,$m); preg_match_all('/<div[^>]*>([^>]*2[^<]*)<\/div>/i',$hm,$m);如果用这个正则来匹配我刚发的那个原始字符串,漏洞就是入如果<div>1 </div> <div><b>1</b>23 </div> <div>4 </div> 这样就匹配不到了.多个b标签 最重要的想法是<div><div>任意字符</div></div> 在"<div>任意字符</div>" 这个任意字符里一定不能够再次出现<div 这个标签 比如[^abcde] 这个正则实际上就是匹配非.但只能针对单个字符.要是我这个单个字符是一个单词呢.就是在这里一定不能出现某个单词如 word 或者中文 "采集" 这样子.把单个字符变成一个单词 那你就先匹配<div>内所有的内容,包括html标签,然后逐个strpos判断下匹配的内容里是否包含'2'.你这样不断的加规则谁也没办法给你确切的建议,规则跨度太大,你可以仔细审核下匹配的具体的规则,1,2,3,4,5列出来,什么什么情况才匹配.. <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>到</tr> 包括tr标签.其实就是匹配<tr></tr>之间不能再有<tr class="*"之类的属性>这个标签,而且这个tr标签里一定包含有采集2个汉字 <div>(?!<div).*?<\/div>楼主知道两个东西就会开朗了.1 (?!<div).* 就是这任意字符里一定不能够再次出现 <div2 ? 限定为非贪婪,只要碰到第一个<\/div> 匹配完成。 求助啊!XML数组存入数据库问题.. 两个下拉框做联动 php菜鸟问题,大家看看 关于php分页 和sql里in的用法的问题 php无法获取提交到本页的数据 PHP + Mysql 插入数据问题 求高亮显示搜索结果中的关键字代码设计思路!关键字都被分了词的。 Linux下Php编程和Windows下的差别大么? 定时在服务器上执行某段程序?????????? 鸟儿兄或其他的高手请进 200分等着你 绝对不难 php如何同时获取远程服务器图片和响应头 PHP提取字段里的img标签
echo htmlspecialchars($m[0][1]);
$reg = "~<div>1 </div> <div>(.*?)</div> <div>4 </div>~";
preg_match_all($reg,$source,$m);
print_r($m);
print_R($m);
preg_match_all('/<div[^>]*>([^>]*2[^<]*)<\/div>/i',$hm,$m);
<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>行
如果用这个正则来匹配我刚发的那个原始字符串,漏洞就是入如果<div>1 </div> <div><b>1</b>23 </div> <div>4 </div> 这样就匹配不到了.多个b标签
你这样不断的加规则谁也没办法给你确切的建议,规则跨度太大,你可以仔细审核下匹配的具体的规则,1,2,3,4,5列出来,什么什么情况才匹配..
这个就是原始字符串.抱歉了
<div>(?!<div).*?<\/div>
楼主知道两个东西就会开朗了.
1 (?!<div).* 就是这任意字符里一定不能够再次出现 <div
2 ? 限定为非贪婪,只要碰到第一个<\/div> 匹配完成。