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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |