LPM: RegEx quiz!

David Hempy hempy at ket.org
Fri Mar 10 15:03:23 CST 2000


Okay, all you macho RegEx wizards...

I want to turn a file path into a URL.  I want to allow filenames like 
"../images/logo.gif".  I will then prepend the project folder, so you get 
something like: "/www/mydepartment/myproject/../images/logo.gif".  Works 
fine for file access, but parent references ("../") do not work in 
URL's.  I need to reduce this to "/www/mydepartment/images/logo.gif".

I thought I had it licked in a single regex, but it balked when you had 
grandparent references ("../../"):

	$url =~ s|/.?/\.\.(?=/)||g;

In the end, I resorted to putting the regex in a loop...seems to work, but 
pains my ego:

	$url = "/a/b/c/u/../v/../d/e/w/../x/y/z/9/../../../../f/index.html";
	print "Start with $url\n";
	while ($url =~ s|/.?/\.\.(?=/)||g) {
		print " Then make $url\n";
	}
	print "  End with $url\n";


Anybody have a leaner solution?

-dave

--
David Hempy
Internet Database Administrator
Kentucky Educational Television
<hempy at ket.org> -- (606)258-7164 -- (800)333-9764




More information about the Lexington-pm mailing list