the worst situation is with replacing /somedir/../ . unfortunately pattern [^/]+/../ does not work as it should. please imagine situation, source path is /../../ and ..... this pattern matches! for complete clean and normalize input path i suggest this procedure:

$path='./anything/../.../something'; #example $path=~s!/+!/!g; #replace //// by single / $path=~s!^\./!!; #remove starting ./ $path=~s!/\.(?=$|/)!!g; #remove all /. 1 while $path =~ s!/([^/]{3,}|[^/.][^/]*|\.[^/.])/\.\.(?=/|$)!!g; #re +move /something/.. $path=~s!^([^/]{3,}|[^/.][^/]*|\.[^/.])/\.\./!!; #remove starting som +ething/../ $path='.' if $path eq ''; #point current path if finally it is empty #at this place $path is normalized

you can wear this code in some function :) for clarification what for is sequence ([^/]{3,}|[^/.][^/]*|\.[^/.]) ? this is something special. this matches to everything names except names that contains '/' character, and does not match to '..' . testing to matching single '.' is unneeded because this has been removed previously. notice, this path fragment matches to '...' and more dots, because only single and double dots are reserved. this provices file names like '.something', usually used as hidden names in unix like systems.

Thank you for the congratulations and I am happy if this piece of code will be help for someone :) I know, I invented wheel again :)


In reply to Re^2: Normalized directory paths by znik
in thread Normalized directory paths by mikfire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.