Solved

Removing everthing after the second last "/"

Posted on 1998-06-13
11
180 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
Industry Leaders: 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!

 

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
collecting information 2 204
Perl Scripting from a shell script with if and for 4 87
perl to convert excel to csv 3 308
Perl Awk Need Help 3 139
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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

733 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