?
Solved

URLs

Posted on 2002-03-05
7
Medium Priority
?
188 Views
Last Modified: 2010-03-05
I have several URLs like
http://domain/dir1/dir4/../dir2/../../dir3/file.html
http://domain/dir1/dir2/../../dir3/file.html
http://domain/dir1/../dir3/file.html
etc..
How do I make them look like
http://domain/dir3/file.html (no ../../)
I guess a one liner can do the job!!
Thanks,
0
Comment
Question by:sridhar_dvjs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:Peewee
ID: 6841579
sridhar_dvjs,
i'm not fully sure what yo want here as your question looks short of information. Perhaps you could explain it a bit more.

However, it seems you would like to get rid of ../ within your URL's.  These characters tell the operating system to go to the preceeding directory.  If you take these out you will may navigate to the desired directory path.

if you wish to take them out you could use a regular expressions.

peewee



0
 
LVL 5

Expert Comment

by:Peewee
ID: 6841584
sridhar_dvjs,

an example regex is below:

#!/usr/bin/perl
print "start\n";
 
my $input = 'http://domain/dir1/dir4/../dir2/../../dir3/file.html';
 
#http://domain/dir1/dir2/../../dir3/file.html
#http://domain/dir1/../dir3/file.html
 
print "$input\n";
$input =~ s/\/\.\.//ig;
print "$input\n";
 
print "end\n";
0
 
LVL 2

Expert Comment

by:windfall
ID: 6841696
Something like this might work:

#!/usr/bin/perl
##########################################################################
@url = ('http://domain/dir1/dir4/../dir2/../../dir3/file.html',
'http://domain/dir1/dir2/../../dir3/file.html',
'http://domain/dir1/../dir3/file.html');

foreach $l(@url){
     
     $l =~ s/(\.\.\/)//gis;
print "$l\n";
}

Hope this helps..
windfall
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 6841906
@url = ('http://domain/dir1/dir4/../dir2/../../dir3/file.html',
'http://domain/dir1/dir2/../../dir3/file.html',
'http://domain/dir1/../dir3/file.html');
foreach( @url ){
    1 while s![^/]+/\.\./!!;
    print "$_\n";
}
#assuming no symbolic links
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6842100
sridhar_dvjs,

"..I have several URLs like..."

Where are these URL's in? Are these URL's inside HTML or other types of files?

If yes, then can you post a sample file here?

"..I guess a one liner can do the job!!..."

Do you have command prompt access to run any command?

Please provide as much more detail as you can.

This will help you get a more accurate answer, faster.
0
 
LVL 1

Expert Comment

by:japhyRPI
ID: 6843129
What the poster is asking is "how can I change a directory path with ../'s in it to the path without ../'s?"  In other words, how can I get from

  /this/that/../foo/bar/blat/../it.txt

to

  /this/foo/bar/it.txt

Here is such code:

  $URL = "http://www.pobox.com/../~japhy/regexes/../

  # remove "leading" ../'s
  $URL =~ s{(http://[^/]+/)(?:\.\.(?:/|$))+}{$1};

  # remove other /..'s
  1 while $URL =~ s{[^/]+/\.\.(?:/|$)}{}g;

This code works with URLs such as "http://www.foo.bar/blat/..", even though the ".." doesn't have a trailing slash.
0
 
LVL 1

Accepted Solution

by:
japhyRPI earned 400 total points
ID: 6843137
Sorry, my line assigning to $URL is missing a closing quote and semicolon.

  $URL = "http://www.pobox.com/../~japhy/regexes/../";
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question