FFSparky has asked for the wisdom of the Perl Monks concerning the following question:
Oh wise ones,
I have 2 string variables (these will be dynamic) containing file system paths. As labeled one string ($BasePath) will contain the base / root path and the other string ($CurrentPath) will be the current path as the script is scanning the file system starting from the base path. What I am after is a string containing just the subdirectory without the base / root path.
Using the working example I have provided the result is what I am after \DirD\DirE
However, I am rather weak with regular expressions thus to me my solution seems to be rough and naturally only deals with the dreaded windows paths. I am hoping there may be a more efficient alternative that could be dynamic and also support unix paths.
use strict; my $BasePath = '\\DirA\DirB\DirC'; my $CurrentPath = '\\DirA\DirB\DirC\DirD\DirE'; $BasePath =~ s/\x5c/\x0d/g; $CurrentPath =~ s/\x5c/\x0d/g; $CurrentPath =~ s/$BasePath//; $CurrentPath =~ s/\x0d/\x5c/g; print "\n\$CurrentPath: ".$CurrentPath."\n";
Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with Regular Expressions
by Riales (Hermit) on Apr 10, 2012 at 19:03 UTC | |
by FFSparky (Acolyte) on Apr 10, 2012 at 19:22 UTC | |
|
Re: Help with Regular Expressions
by johngg (Canon) on Apr 10, 2012 at 22:18 UTC | |
|
Re: Help with Regular Expressions
by nemesdani (Friar) on Apr 10, 2012 at 19:14 UTC | |
by FFSparky (Acolyte) on Apr 10, 2012 at 19:56 UTC | |
by nemesdani (Friar) on Apr 10, 2012 at 20:44 UTC | |
|
Re: Help with Regular Expressions
by Anonymous Monk on Apr 11, 2012 at 09:28 UTC | |
by FFSparky (Acolyte) on Apr 11, 2012 at 14:00 UTC |