Re: need help with a regular expression



Alex Walker wrote:
> 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.

That's simply not possible with the code you showed. Post a
short-but-complete script that demonstrates the problem you're having.
For example:
#!/usr/bin/env perl
use strict;
use warnings;


# slurp the file
my $fileString = do { local $/; <DATA> };
# 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 $fileString;

__DATA__
<!-- 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>
border=4></p>


Output:
<!-- example 1 - with map -->

<div align="center">
<div id="mapit"><p class=imagecenter><img src="screen1.gif"
x-maintain-ratio=TRUE
usemap=#MAP69764000
width=496
height=279
border=4></p>
</div>

<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>

> I'm a little stuck on this one, any help would be much appreciated.

You have clearly misdiagnosed your problem. We can't help you until
you help yourself by creating a short-but-complete script that still
demonstrates your problem.

Paul Lalli

.