Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Unexpected Error: \C no longer supported in regex (updated x2)

by AnomalousMonk (Archbishop)
on Aug 20, 2022 at 02:31 UTC ( #11146251=note: print w/replies, xml ) Need Help??


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

In general, a \ (backslash) introduces a regex zero-width assertion. Some such are \A \B \b \Z \z and there are others. Why do you need backslashes in your paths? Even in Perl running under Windows, forwardslashes work just as well unless you need to construct a command that is to be given to the Windows shell. I don't recall what assertion \C used to be, but clearly Perl does.

Update 1:

... in m/C:\Book\ <-- HERE C0001-Chapter-001.mp3/ ...
Note that the \B assertion is also present in the
    m/C:\Book\C0001-Chapter-001.mp3/
regex with which you are trying to construct a match for a file name, so the regex will not match what you think it will even if \C was not a problem. Would a simple string equality eq comparator be better here? (Update: Oops... Forgot you want to do a substitution.)

Update 2: See quotemeta:

Win8 Strawberry 5.8.9.5 (32) Fri 08/19/2022 19:42:14 C:\@Work\Perl\monks >perl use strict; use warnings; my $txt = "C:\\Book\\C0001-Chapter-001.mp3"; my $new = "C:\\Book\\NEW-C0001-Chapter-001.mp3"; print "\n"; print "Before: '$txt' \n"; # Workaround -- use \Q \E -- see quotemeta. $txt =~ s/\Q$txt\E/$new/; print "After: '$txt' \n"; print "\n"; ^Z Before: 'C:\Book\C0001-Chapter-001.mp3' After: 'C:\Book\NEW-C0001-Chapter-001.mp3'


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Unexpected Error: \C no longer supported in regex (updated x2)
by roho (Chancellor) on Aug 20, 2022 at 03:49 UTC
    Thank you AnomalousMonk! I forgot about quotemeta. That was what I needed.

    Regarding the backslashes, I know Perl can use forward slashes just fine, even on Windows, but there are other apps that reference these files that require the Windows standard backslashes.

    Thanks again for your quick reply.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2023-02-05 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?