Solved

Removing everthing after the second last "/"

Posted on 1998-06-13
11
174 Views
Last Modified: 2011-09-20
Hi,
     I've got a path and I'd like drop down one dir by simply editing the string.  So I want to be able to change /u3/user/public_html/otherdir/ to /u3/user/public_html/ how would I do this easily?
0
Comment
Question by:cide
  • 5
  • 3
  • 3
11 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 50 total points
ID: 1207837
The easy way is:

$path = "/u3/user/public_html/otherdir/";
$path =~ s![^/]+/$!!;
print "$path\n";

Will print:
/u3/user/public_html/

This regexp assumes that the string ends in /, and the directory separator will always be /, which your question implies.

Hope this solves your problem, good luck!
0
 
LVL 6

Expert Comment

by:alamo
ID: 1207838
Just to explain a little more:

The regular expression "[^/]+/$" matches any character other than / (the "[^/]" part), one or more times (the "+" part"), ending in a "/" at the end of the line (the "$" part).

The "+" part will attempt to match as many characters as it can, which is how it matches the entire last directory name.
0
 

Author Comment

by:cide
ID: 1207839
Nice work, thanks a bunch! :)
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:cide
ID: 1207840
Nice work, thanks a bunch! :)
0
 
LVL 2

Expert Comment

by:tpryor
ID: 1207841
$path = "/u3/user/public_html/otherdir/";

$path =~ /(^.*\/).+\/;
$new_path = $1;

this will work for path's with trailing slashes or not.  ex: "/u3/user/public_html/otherdir"  or
"/u3/user/public_html/otherdir/"

$new_path will allways be shorter by one dir.

GL,
t
0
 

Author Comment

by:cide
ID: 1207842
Interesting! :)
Regexp really make perl useful.
0
 

Author Comment

by:cide
ID: 1207843
Interesting! :)
Regexp really make perl useful.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1207844
Yes, very much so cide. It's amazing what a regexp can do - the only downside is they can be somewhat cryptic and tricky to debug when they don't work.

By the way tpryor, your last regexp gave "Search pattern not terminated" when I tried it.
0
 

Author Comment

by:cide
ID: 1207845
Interesting! :)
Regexp really make perl useful.
0
 
LVL 2

Expert Comment

by:tpryor
ID: 1207846
those \ chars are actually backslashes

it will do the trick

t
0
 
LVL 2

Expert Comment

by:tpryor
ID: 1207847
oops, somethin got copied wrong.
should be...

$path =~ /(^.*\/).+$/;
$new_path = $1;

or

$path =~ s/(^.*\/).+$/$1/;

that will do it.

regex gotta love it!
t
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

828 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