Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

remove the part of the string that is after the second to last '/'

by einerwitzen (Sexton)
on Mar 07, 2002 at 21:44 UTC ( [id://150141]=perlquestion: print w/replies, xml ) Need Help??

einerwitzen has asked for the wisdom of the Perl Monks concerning the following question:

I have a string "$var = "one/two/three/four/five/six/";"
(note the last ' / ' on it. I need to be able to remove just the last item, leaving the rest as a string,
"$var2 = "one/two/three/four/five/";

Is there a simple way to do this?

Replies are listed 'Best First'.
Re: remove the part of the string that is after the second to last '/'
by dragonchild (Archbishop) on Mar 07, 2002 at 21:46 UTC
    $var =~ s![^/]*/$!!;

    Have fun! :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: remove the part of the string that is after the second to last '/'
by virtualsue (Vicar) on Mar 07, 2002 at 23:20 UTC
    There are lots of ways to do it, and this thread will likely fill up with them. I can't help noticing that what you're looking for resembles the unix command "dirname", however.
    chomp($str = `dirname $str`);
    Yes, it's silly and not portable. I'm just covering a base here. :-)

    The dirname method in File::Basename would be a good choice if you are in fact manipulating directory paths. This module is reasonably platform independent and part of the core Perl distribution.

    use File::Basename; my $dir = dirname($filename);
    Do "perldoc File::Basename" for more information.
Re: remove the part of the string that is after the second to last '/'
by ajwans (Scribe) on Mar 07, 2002 at 21:46 UTC
    How about

    $string =~ s!^(.*/).*?/$!$1!;

    Guess I just prefer non-greedy matching to negated character classes.

    1. dude, what does mine say?
    2. "sweet", what about mine?
    3. "dude", what does mine say?
    4. GOTO 2
Re: remove the part of the string that is after the second to last '/'
by abaxaba (Hermit) on Mar 08, 2002 at 06:52 UTC
    #If you don't need the original string: $var =~ s|[^/]+/$||; #If you would like to leave $var unchanged: ($newVar) = $var=~ m|(.+/)[^/]+/$|;
Re: remove the part of the string that is after the second to last '/'
by SgtClueLs (Novice) on Mar 07, 2002 at 22:33 UTC
    Or the non cool way! Regular expression are for weenies! :)
    $var = "one/two/three/four/five/six/"; split /\//, $var; pop; $var = $var . $_ foreach(@_);
      You weenie! You used a regular expression! :)

      I think I like this way of avoiding regexes better:
      substr($var, rindex($var, '/', length($var) - 2)) = '';

      ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
      Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
      -- vs lbh hfrq OFQ pnrfne ;)
          - Whreq
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://150141]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2025-06-18 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.