Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: comp.lang.perl Subject: Re: Simplifying paths. A hairy regex Message-ID: <19249:Mar223:57:2891@kramden.acf.nyu.edu> Date: 2 Mar 91 23:57:28 GMT References: <11249@cae780.csi.com> <1991Mar2.202521.29658@iwarp.intel.com> Organization: IR Lines: 15 X-Right-Newsgroups: comp.programming In article <1991Mar2.202521.29658@iwarp.intel.com> merlyn@iwarp.intel.com (Randal L. Schwartz) writes: > Hmm. after writing this code, I know it breaks on "../..". Yuck. > But I'm late for my next appointment. The flash I just had (if > someone wants to make it work) is to have an inviolate "prefix string" > consisting of all the null and ".." entries from the head of the > string as in /^((\.\.)?\/)+/, and then push and pop the rest as above. No, that fails. foo/../../bar. You'll have to keep track of the number of leading ..'s, then apply your solution, increasing the number of ..'s by one every time you pop an empty stack. I think the regexp version is more natural; I basically did it that way when I wrote the same routine in C a month back. ---Dan