senthilv has asked for the wisdom of the Perl Monks concerning the following question:

I am not able to remove ^M from an uploaded file using the following regular expression...
s/\^M//; s/\^M$//; s/^M//; s/\x0D//;
^M is visible only in Vi editors ...

Replies are listed 'Best First'.
Re: Regular expression to remove ^M
by ikegami (Patriarch) on Jan 20, 2007 at 10:47 UTC

    ^M is a representation for Carriage Return. s/\x0D//g; should have worked. s/\r//g; is another way.

Re: Regular expression to remove ^M
by Zaxo (Archbishop) on Jan 20, 2007 at 20:55 UTC

    ^M and \^M take on different meanings in regexen. The first looks for a capitol 'M' as the first character in the string. The second looks for literal '^M' -- two characters.

    ikegami shows you several ways to do that, there are others. '\cM' is perhaps the closest to what you are trying. For portability to mac, '\r' is likely the best choice.

    Instead of substitution, try transliteration,

    tr/\r//d;
    It's somewhat faster, and is equivalent to s/\r//g;.

    After Compline,
    Zaxo