正则表达 - 获得一切但最后的匹配

时间:2010-09-02 19:36:39

标签: php regex

我有以下HTML:

<p><a href="http://vimeo.com/13114334" title="Grain & Gram: Nick Sambrato, Printmaker"><img src="http://b.vimeocdn.com/ts/747/476/74747630_200.jpg" alt="Grain & Gram: Nick Sambrato, Printmaker" /></a></p>
<p>Read the full interview with Nick Sambrato, Printmaker here:<br /><br /><a href="http://grainandgram.com/nicksambrato/" target="_blank" rel="nofollow">grainandgram.com/nicksambrato/</a></p>
<p>Cast: <a href="http://vimeo.com/grainandgram" style="color: #2786c2; text-decoration: none;">Grain & Gram</a></p>

我的目标是隔离最后一组段落标记。我正在尝试匹配段落标签之间的所有内容。我希望我得到三个结果,我可以操纵数据。

我尝试了以下正则表达式:

<p\b[^>]*>(.*?)<\/p>

它只与第一组段落标记匹配。如何让它与前两个匹配?

由于

更新 我以错误的方式思考这个问题。我不能总是假设在我想要的Cast文本之前会有X量的信息。不过,我可以假设Cast将是最后一段。所以修改后的问题:除了最后一段以外,我怎样才能匹配所有内容?换句话说,如何在“&lt; p&gt; Cast:”之前匹配所有内容?

1 个答案:

答案 0 :(得分:1)

我想你只需说你想要多个:

(<p\b[^>]*>(.*?)<\/p>)*

或者您可以使用原始正则表达式并使用preg_match_all,然后选择最后一个元素。

编辑回复:如何(<p\b[^>]*>(.*?)<\/p>)*<p>Cast

相关问题