On Wed, 23 Mar 2005 17:06:59 +0100, Jan Eden wrote:
> Hi,
> 
> I use the following regex to split a (really simple) file into sections 
> headed by <h1>.+?</h1>:
> 
> while ($content =~ m#<h1>(.+?)</h1>(.+?)(?=<h1>)#gs) {
>     ...
> }
> 
> This works perfectly, but obviously does not catch the last section, as it is 
> not followed 
> by <h1>.
> 
> How can I catch the last section without
> 
> * doing a separate match for it
> * loosing the convenience of the g switch to wade through the whole file?
> 
> Thanks,
> 
> Jan

Change your RE to:
m#<h1>(.+?)</h1>(.+?)(?=<h1>|$)#gs

In other words, look ahead to either a <h1> or the end of the string ("$").
I have to admit this problem wasn't as simple as I initially thought -
I still have no idea why my first guess didn't work:
m#<h1>(.+?)</h1>(.+?)(?=<h1>)?#gs

Maybe someone with more knowledge of REs can answer?

Regards,
-- 
Offer Kaye

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to