Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Trimming Paths with regexes

by Hofmator (Curate)
on Aug 30, 2001 at 14:43 UTC ( [id://109039]=note: print w/replies, xml ) Need Help??


in reply to Trimming Paths with regexes

Just to show that it's not too complicated with a regex:

do { # your stuff here print "\t\t$path\n"; } while $path =~ s#(?:(^/)|/)[^/]+$#$1?$1:''#e;
You can use a subroutine as well, then the regex is pretty simple if you handle the special case beforehand:
sub pathtrim { my ($path) = @_; return '/' if ($path =~ m#^/[^/]+$/); $path =~ s#/[^/]+$##; return $path; }

Update: Fixed small bug in the subroutine.

-- Hofmator

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2025-07-17 12:40 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.