Solved

Removing everthing after the second last "/"

Posted on 1998-06-13
11
159 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do I grep for a specfic DNS record in a directory 3 42
SIMPLE Perl Regex 1 153
problem with using the glob function 1 63
Bash one liner, start and end time calculation 13 103
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

867 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

19 Experts available now in Live!

Get 1:1 Help Now