SPUG: Re: regex problems...

Sean seanow0 at email.msn.com
Fri Oct 1 16:26:34 CDT 1999


>In fact, could you post the actual code, plus the actual line in the text
you're attempting to replace?

OK, here's my code; and after a lot of trial & error I've actually found
something that works:

Here's the snippet of HTML I'm trying to find:
<A HREF="../seattle.html?rm=XX" CLASS="text">Seattle</A>

and here's what I want when I finish:
<A HREF="../seattle.html?rm=XX" CLASS="highlighttext"><img
src="left.gif">Seattle<img src="right.gif"></A>

XX is a variable that changes with different categories but needs to be kept
after the expression.  And 'Seattle' could be anything -- and doesn't
necessarily match the filename

Here's my code:

/usr/bin/perl -w

# open the file and get my code.  just pretend here, since I know that part.
assume that $code equals the html code of a file.

# get user input for 'seattle' filename and link text
print "Display name to get image: ";
chop($getimage=<STDIN>);
print "Exact filename to get image: ";
chop($getimagefile=<STDIN>);
print "\n";

#assign variables to static code.  yes some are dupes, but its easier to
track
$beforea='<A HREF="../';
$beforeb='.html?rm=';
$beforec='" CLASS="text">';

$aftera='<A HREF="../';
$afterb='.html?rm=';
$afterc='" CLASS="highlighttext"><img src="left.gif">';
$afterd='<img src="right.gif">';

#run the expression to change the name the user requested to add an image &
change the style (as shown in the code above) but keep the XX variable

$code =~
s/($beforea)($getimagefile)($beforeb)(\w\w)($beforec)($getimage)/$aftera$get
imagefile$afterb$4$afterc$getimage$afterd/gsi;

print "$code";

So I figured out two things:
1. is that I can use parentheses on the left side of the expression but not
the right (shown above)
2. is that I've found (\w\w) doesn't allow the expression to work -- instead
I assigned a variable to equal '\w\w' and used that in it's place
3. is that my variables need to have special characters escaped on the left,
but not escaped on the right (not shown above, the code above I think should
work, but don't understand why it doesn't).

Is it really necessary to change my $after variables to escape the '.' and
'?'  ?  Or is there a cleaner way of doing this?  The main reason for my
asking is not only do I need to add images, but I also need to take them
away, so I end up with 4 sets of variables, two escaped, and two unescaped.
Very messy!

Thanks again



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list