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.


In reply to Help with Regular Expressions by FFSparky

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.