in reply to Unexpected Error: \C no longer supported in regex

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.

Replies are listed 'Best First'.
Re^2: Unexpected Error: \C no longer supported in regex
by roho (Bishop) on Aug 22, 2022 at 05:45 UTC
    Thank you for the alternative approach atcroft. My sample code is an exact representation of what I was processing. It was a simple, straightforward (or so I thought) substitution in a string that happened to be a file path, and the part of the path that contained "\C" was the culprit, as AnomalousMonk pointed out. One of the things I've always liked about Perl is timtowtdi. Thanks again for your input.

    "It's not how hard you work, it's how much you get done."