Solved

URLs

Posted on 2002-03-05
7
181 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
7 Comments
 
LVL 5

Expert Comment

by:Peewee
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
Comment Utility
@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
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
Sorry, my line assigning to $URL is missing a closing quote and semicolon.

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now