I've run across an unexpected error in a regex substitution. The text to be modified is a directory path in Windows with backslashes. The file name starts with "C", so when the regex sees the final directory path backslash followed by the "C" in the filename it returns the following error message:

\C no longer supported in regex; marked by <-- HERE in m/C:\Book\ <-- HERE C0001-Chapter-001.mp3/ at C:\tmpx\xx.pl line 15.

I have a workaround, which is to change the backslashes to forward slashes before performing the regex substitution, then changing the forward slashes back to backslashes, which seems kind of kludgy.

Any ideas on how to overcome the "\C" error message? I have tried running the text through "qr" but that does not help. I have tried both "e" and "ee" regex suffixes but that does not help either.

In the following code, comment out the "Workaround" lines that change backslashes to forward slashes to see the error.

#!/usr/bin/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 -- Change backslashes to forward slashes. $txt =~ s/\\/\//g; $new =~ s/\\/\//g; $txt =~ s/$txt/$new/; # Workaround -- Change forward slashes back to baskslashes. $txt =~ s/\//\\/g; $new =~ s/\//\\/g; print "After: $txt\n"; print "\n";

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


In reply to 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.