I understand the question and the problem you found, but I feel like it is a sign you are using the wrong tool for the job. Not only does perl understand both forward ('/') and back ('\') slashes as path separators on most platforms (and you don't have to escape them if you use single-quotes, by the way), but it includes File::Spec::Win32 in the core, which appears to be in perl on multiple platforms. (I checked and it appears to be present on the Windows/Strawberry, Windows/Cygwin, and Linux/Fedora systems I checked with perl installed.) File::Basename is another core module that can separate file names and paths, to might allow you to work on just the piece you want (but I haven't thought through that one in this case).

I know your code is a simple example (and thus may not reflect *WHY* you are trying to do this), but consider the following code to do something similar.

# File::Spec::Win32 example # #!/usr/bin/perl use strict; use warnings; use File::Spec::Win32; my @txt = ( 'C:\Book\C0001-Chapter-001.mp3', ); my @re = ( [ qr{^(C\d+)}msx => '"NEW-$1"', ], [ qr{(-Ch)}msx => 'lc("$1")', ], ); foreach my $fn ( @text ) { print qq{Before: }, $fn, qq{\n}; # # @fp - file parts (volume, directory, filename) # @dl - directory path to $fn my @fp = File::Spec::Win32->splitpath( $fn ); my @dl = File::Spec::Win32->splitdir( $fp[1] ); # # Apply regexes to file name # (could also add logic to modify some part of the # directory path in @dl as well). foreach my $regex ( @re ) { $fp[3] =~ s/$regex->[0]/$regex->[1]/gee; } # # Rebuild file name $fp[1] = File::Spec::Win32->catdir( @dl ); my $ffn = File::Spec::Win32->catpath( @fp ); print qq{After: }, $ffn, qq{\n}; } # # Output: # # Before: C:\Book\C000-Chapter-001.mp3 # After: C:\Book\NEW-C000-chapter-001.mp3

Hope that helps.


In reply to Re: Unexpected Error: \C no longer supported in regex by atcroft
in thread Unexpected Error: \C no longer supported in regex by roho

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.