I would recommend using a module for manipulating paths. File::Spec is a core module, meaning you normally don't have to install it, and Path::Class is basically a nicer interface for it that gives you objects with lots of nice features.

use File::Spec::Functions qw/abs2rel/; my $path = "C:\\Dir1\\Dir2\\Dir3\\KeepMe"; my $base = "C:\\Dir1\\Dir2\\Dir3"; print abs2rel($path, $base), "\n"; # prints "KeepMe" # - or - use Path::Class qw/dir file/; my $path = dir('C:\\','Dir1','Dir2','Dir3','KeepMe'); my $base = dir('C:\\','Dir1','Dir2','Dir3'); print $path->relative($base), "\n"; # prints "KeepMe"

This code assumes it is being run on a Windows system - if you are instead manipulating Windows pathnames on a Linux or Mac system, there are ways to do that with the above modues as well.

s/$find/$replace/eeg

BTW, why are you using /eeg here? It doesn't seem necessary. (Plus, I would also anchor the regex to the beginning of the string with ^, just in case.)

Update: You asked the same question 11 years ago: Help with Regular Expressions


In reply to Re: Help with regex using variables by haukex
in thread Help with regex using variables 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.