正则匹配 部分符合要求的数据匹配不到

军军呀 发布于 2017/01/03 17:13
阅读 218
收藏 0

情况是这样的 比如有这样一个字符串 

"<li><a>123</a>|<a>321</a></li> 

 <li><a>456</a>|<a>654</a></li>"

我写的正则 可以匹配到 <a>123</a>和<a>456</a>

但是无法匹配到 <a>321</a>和<a>654</a>

求大神给指点

以下是 原本的程序 以及运行结果

$pattern = "/<a[^>]+href=['\"].*['\"][^>]>.+?<\/a>/";
$pattern1 = "/<a[^>]+>.+?<\/a>/";
//$pattern = "/<a[^>]+href=['\"].*?['\"][^>]+>\w*<\/a>/";
// <a[^>]+>\w*</a>|
$subject = '
<div style="margin:0;padding:0;height:0;clear:both;font-size:0px;overflow:hidden"></div><div id="servicetab" class="g-br3 g-bg5 servicetab" monkey="mxlm"><div class="servicetab-bd"><div id="box-starbar" class="starbar" monkey="starbar"><ul><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-tv">
</i><a href="http://v.hao123.com/dianshi/" title="电视剧" class="baritem-link " >电视剧</a><span class="split">|</span><a href="http://v.hao123.com/zongyi/" title="综艺" class=" baritem-link">综艺</a></div></div>
</li><li class="startbar-list">\'<div class="g_clr baritem"><div class="category"><i class="star-icon i-game"></i><a href="http://game.hao123.com/" title="游戏" class="baritem-link " >游戏</a><span class="split">|</span><a href="http://xyx.hao123.com/" title="小游戏" class=" baritem-link">小游戏</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-movie"></i><a href="http://v.hao123.com/dianying/" title="电影" class="baritem-link " >电影</a><span class="split">|</span><a href="http://live.hao123.com/" title="直播" class=" baritem-link">直播</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-comic"></i><a href="http://v.hao123.com/dongman/" title="动画" class="baritem-link " >动画</a><span class="split">|</span><a href="http://www.hao123.com/manhua" title="漫画" class=" baritem-link">漫画</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-toutiao"></i><a href="http://news.hao123.com/" title="新闻" class="baritem-link " >新闻</a><span class="split">|</span><a href="http://tuijian.hao123.com/?type=military" title="军事" class=" baritem-link">军事</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-travel g_red"></i><a href="http://go.hao123.com/sales?tn=mxlm" title="旅游" class="baritem-link  g_red" >旅游</a><span class="split">|</span><a href="http://music.hao123.com/" title="音乐" class=" baritem-link">音乐</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-caipiao"></i><a href="http://caipiao.hao123.com/" title="彩票" class="baritem-link " >彩票</a><span class="split">|</span><a href="http://jingcai.hao123.com" title="竞猜" class=" baritem-link">竞猜</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-gaoxiao"></i><a href="http://www.hao123.com/gaoxiao" title="搞笑" class="baritem-link " >搞笑</a><span class="split">|</span><a href="http://book.hao123.com" title="小说" class=" baritem-link">小说</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-tuan"></i><a href="http://tejia.hao123.com/?tn=mxlm" title="特价" class="baritem-link " >特价</a><span class="split">|</span><a href="http://shouji.hao123.com/?tn=mxlm" title="手机" class=" baritem-link">手机</a></div></div>
</li><li class="startbar-list"><div class="g_clr baritem"><div class="category"><i class="star-icon i-music"></i><a href="http://www.hao123.com/stocknew" title="股票" class="baritem-link " >股票</a><span class="split">|</span><a href="http://8.hao123.com/" title="理财" class=" baritem-link">理财</a></div></div>
</li></ul></div><div class="box-startools" data-hook="box-startools" monkey="tools"><i class="startools-icon i-life"></i><a href="#" class="startools-arrow" data-hook="startools-arrow" alog-text="展开"></a><div class="startools-container box-toolsDown" data-hook="startools-container" ><div class="startools-list"><a href="http://www.weather';


preg_match_all( $pattern, $subject ,$out);
preg_match_all( $pattern1, $subject ,$out1);
print_r($out);
print_r($out1);




运行结果

Array
(
    [0] => Array
        (
            [0] => <a href="http://v.hao123.com/dianshi/" title="电视剧" class="baritem-link " >电视剧</a>
            [1] => <a href="http://game.hao123.com/" title="游戏" class="baritem-link " >游戏</a>
            [2] => <a href="http://v.hao123.com/dianying/" title="电影" class="baritem-link " >电影</a>
            [3] => <a href="http://v.hao123.com/dongman/" title="动画" class="baritem-link " >动画</a>
            [4] => <a href="http://news.hao123.com/" title="新闻" class="baritem-link " >新闻</a>
            [5] => <a href="http://go.hao123.com/sales?tn=mxlm" title="旅游" class="baritem-link  g_red" >旅游</a>
            [6] => <a href="http://caipiao.hao123.com/" title="彩票" class="baritem-link " >彩票</a>
            [7] => <a href="http://www.hao123.com/gaoxiao" title="搞笑" class="baritem-link " >搞笑</a>
            [8] => <a href="http://tejia.hao123.com/?tn=mxlm" title="特价" class="baritem-link " >特价</a>
            [9] => <a href="http://www.hao123.com/stocknew" title="股票" class="baritem-link " >股票</a>
        )

)
Array
(
    [0] => Array
        (
            [0] => <a href="http://v.hao123.com/dianshi/" title="电视剧" class="baritem-link " >电视剧</a>
            [1] => <a href="http://v.hao123.com/zongyi/" title="综艺" class=" baritem-link">综艺</a>
            [2] => <a href="http://game.hao123.com/" title="游戏" class="baritem-link " >游戏</a>
            [3] => <a href="http://xyx.hao123.com/" title="小游戏" class=" baritem-link">小游戏</a>
            [4] => <a href="http://v.hao123.com/dianying/" title="电影" class="baritem-link " >电影</a>
            [5] => <a href="http://live.hao123.com/" title="直播" class=" baritem-link">直播</a>
            [6] => <a href="http://v.hao123.com/dongman/" title="动画" class="baritem-link " >动画</a>
            [7] => <a href="http://www.hao123.com/manhua" title="漫画" class=" baritem-link">漫画</a>
            [8] => <a href="http://news.hao123.com/" title="新闻" class="baritem-link " >新闻</a>
            [9] => <a href="http://tuijian.hao123.com/?type=military" title="军事" class=" baritem-link">军事</a>
            [10] => <a href="http://go.hao123.com/sales?tn=mxlm" title="旅游" class="baritem-link  g_red" >旅游</a>
            [11] => <a href="http://music.hao123.com/" title="音乐" class=" baritem-link">音乐</a>
            [12] => <a href="http://caipiao.hao123.com/" title="彩票" class="baritem-link " >彩票</a>
            [13] => <a href="http://jingcai.hao123.com" title="竞猜" class=" baritem-link">竞猜</a>
            [14] => <a href="http://www.hao123.com/gaoxiao" title="搞笑" class="baritem-link " >搞笑</a>
            [15] => <a href="http://book.hao123.com" title="小说" class=" baritem-link">小说</a>
            [16] => <a href="http://tejia.hao123.com/?tn=mxlm" title="特价" class="baritem-link " >特价</a>
            [17] => <a href="http://shouji.hao123.com/?tn=mxlm" title="手机" class=" baritem-link">手机</a>
            [18] => <a href="http://www.hao123.com/stocknew" title="股票" class="baritem-link " >股票</a>
            [19] => <a href="http://8.hao123.com/" title="理财" class=" baritem-link">理财</a>
        )

)



加载中
0
littledew
littledew

你的问题信息量太多,get不到重点,建议提炼问题。

0
张凯乐
张凯乐

你写的正则表达式有些问题(比如“.*”需要改为“.*?”,如果未指定 DOTALL 标志,则正则表达式 . 可以与任何字符(行结束符除外)匹配。),没必要写那么多,使用贪婪数量词,试试这个正则表达式:

(?s)<a.*?>.*?</a>


返回顶部
顶部