For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > need help with a regular expression









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author need help with a regular expression
Alex Walker

2006-01-30, 6:56 pm

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 cl
oses. 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.
Xicheng

2006-01-30, 6:56 pm

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;


check this up, you have two opening <div> tags in your replacement
part.....

> # 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....
I didnt see this result though.

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


Paul Lalli

2006-01-30, 9:55 pm

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com