V. Arora
V. Arora

Reputation: 53

Regular Expression - Getting Everything But Last Match

I have the following bit of 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>

My goal is to isolate the last set of paragraph tags. I'm trying this by match everything between the paragraph tags. My hope was that I'd get three results and I could manipulate the data.

I tried the following regular expression:

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

It's only matching with the first set of paragraph tags. How do I get it to match with the first two?

Thanks

Update: I was thinking about this in the wrong way. I can't always assume that there will be X amount of information before the Cast text that I want. I can assume that Cast will be the last paragraph pulled, however. So the revised question: How can I match with everything EXCEPT the last paragraph? In other words, how do I match everything before "<p>Cast:"?

Upvotes: 1

Views: 355

Answers (1)

Scott Stafford
Scott Stafford

Reputation: 44808

I think you just need to say you want multiple of those:

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

Or you can use your original regex and use preg_match_all, and pick the last element.

EDIT RESPONSE: Howabout (<p\b[^>]*>(.*?)<\/p>)*<p>Cast?

Upvotes: 1

Related Questions