Click to See Complete Forum and Search --> : regexp issue


afmook
10-18-2005, 09:03 AM
Looked online a bit... These things are so confusing.

I'm making a cgi script that will process other files. IE you pass a "page" var to it and it'll open that page, run through and find whatever I want, translate stuff and spit it back.

One of the things I want to fix is that the images aren't displaying because they aren't in the CGI directory. I don't want to have to enter huge url's for each image, nor clutter the cgi-bin, so I figured I'd run a quick translation.

The problem is that it's not quite working as expected.

if ($z =~ m/src=\".+\.(jpg|jpeg|gif|bmp)\"/) {

I'm using this to find any *.jpg, *.gif, etc. And that part works, but what I need is a way to reference the * part, the actual filename without the extension.

I thought you could do $1 for it, but that gives me "gif" or whatever the second part is. Figuring I screwed up I tried $0 but that gives me the cgi script's name.

How do you reference those properly?

Thx a bunch.

fireartist
10-18-2005, 10:07 AM
Add another pair of capturing parenthesis ( )

if ($z =~ m/src=\"(.+)\.(jpg|jpeg|gif|bmp)\"/) {}

$1 now gives the filename
$2 gives the extension

afmook
10-18-2005, 05:48 PM
Awesome, thanks!

...but unfortunately I'm stuck again. How do I get it to put a ../ in front of the old one? I tried


$z =~ s/src=\"(.+)\.(jpg|jpeg|gif|bmp)\"/src=\"../$1\.$2\"/;


also tried the same thing with tr/ instead... I'm still getting used to these things.

But I get an error:

syntax error at basic.cgi line 48, near "s/src=\"(.+)\.(jpg|jpeg|gif|bmp)\"/src=\"../$1"
Can't find string terminator '"' anywhere before EOF at basic.cgi line 48.


Reason I'm doing this is that the images are located one folder up (ie, a "cd.." cmd, or putting a "../" in the filename.) and I simply want it to add that prefix so the images display.

Thanks again for help.

Nedals
10-18-2005, 10:20 PM
my $z = 'src="imagename.gif"';
if ($z =~ m/src=\"(.+)\.(jpg|jpeg|gif|bmp)\"/) { ## by fireartist
print "Name: $1" . "\n";
print "Ext: $2" . "\n";

# now do what? Add ../ in front?
print "src=../$1.$2" . "\n";
}

Or am I mis-understanding the problem.

fireartist
10-19-2005, 03:59 AM
Or just escape the / in ../

$z =~ s/src=\"(.+)\.(jpg|jpeg|gif|bmp)\"/src=\"..\/$1\.$2\"/;

afmook
10-20-2005, 11:42 AM
Fixed it, thanks!