Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Removing everthing after the second last "/"

Posted on 1998-06-13
11
Medium Priority
?
190 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
[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
  • 5
  • 3
  • 3
11 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
Six Sigma Control Plans

636 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