Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!lll-winken!elroy.jpl.nasa.gov!ames!cae780!muir From: muir@cae780.csi.com (David Muir Sharnoff) Newsgroups: comp.lang.perl Subject: Simplifying paths. A hairy regex Message-ID: <11249@cae780.csi.com> Date: 2 Mar 91 06:17:25 GMT Reply-To: muir@csi.com (David Muir Sharnoff) Organization: Comdisco Systems Inc., Foster City, CA Lines: 33 It's 10pm, everyone else has gone home, I just have to share this... The most twisted regex that I've had to build. I wanted to get rid of extra junk from unix filenames. To that end, I tranform: a//c -> a/c a/./c -> a/c a/c/. -> a/c a/b/../c -> a/c a/c/d/.. -> a/c Note: I do not tranform //a/c -> /a/c as that would break Apollo filenames. ------------ perl starts here ---------- sub simplify { for $p (@_) { while($p =~ s!(/\.(/))|(^(.+/)/)|(/\.$)|([^/.]+/\.\./)|(/[^/.]+/\.\.$)!\2\4!) {;} } } ------------ perl ends here ---------- Please let me know if I overlooked something... This is production code and I support it in-house. $,= ' ';$japh = "Just/not/really/../../another/././perl/hacker,/./today/../."; while($japh =~ s!(/\.(/))|(^(.+/)/)|(/\.$)|([^/.]+/\.\./)|(/[^/.]+/\.\.$)!\2\4!) {;}; print (split(m,/+,,$japh))[1-4]; -- David Muir Sharnoff. "RISC is about one year ahead" muir@csi.com (415) 358-3664 (415) 644-0441 Comdisco Systems Inc. 919 East Hillsdale Blvd, Foster City, CA 94404