need help with a regular expression



I'm a little stuck on a regular expression. What I want to do is slurp in an HTML file and insert DIV tags around code that meets certain requirements. Here's a sample of what I'm processing:

<!-- example 1 - with map -->
<p class=imagecenter><img src="screen1.gif"
x-maintain-ratio=TRUE
usemap=#MAP69764000
width=496
height=279
border=4></p>

<h2 class=link><a name="Select Report Parameters Screen1"></a>NO MAP</h2>

<!-- example 2 - no map -->
<p class=ImageCenter><img src="16_report_date.gif"
x-maintain-ratio=TRUE
width=496
height=295
border=4></p>


The code I'm looking for should fall within the Imagecenter tags but should only apply to the images that have a "usemap" tag (like the first image in the example). Here's the applicable part of my program:

# slurp the file
my $fileString = do { local $/; <INFILE> };
# close the file
close(INFILE);
# look for image maps
$fileString =~ s/(<p class=imagecenter>.*?<img.*?usemap.*?<\/p>)/\n<div align=\"center\">\n<div id=\"mapit\">$1\n<\/div>/igs;
# print to outfile
print OUTFILE $fileString;

Everything works great when there is a "usemap" tag, but problems arise on the others. When it his an "imagecenter" paragraph without a "usemap" tag, it does not skip the parapragh. Instead it puts the opening <div> tag at the front and then just never closes. I'm a little stuck on this one, any help would be much appreciated.

Thanks,
Alex


---------------------------------
Bring words and photos together (easily) with
PhotoMail - it's free and works with your Yahoo! Mail.