http://www.perlmonks.org?node_id=11111840


in reply to New File Not Working

NB: The  \1 \2 \3 notation is for regex back-references and is not kosher in a  s/// replacement string. warnings would have told you this had you enabled them. This (ab)use of back-references is a special dispensation of Perl, but should be avoided in favor of capture variables; see Variables related to regular expressions in perlvar.

c:\@Work\Perl\monks>perl -wMstrict -le "use strict; use warnings; ;; my $line = 'OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/ +30/2019,2015-06-20 00:00:00'; ;; $line =~ s/(\d{4})-(\d{2})-(\d{2})\s\d{2}:\d{2}:\d{2}/\2\/\3\/\1/g; print qq{>$line<}; " \2 better written as $2 at -e line 1. \3 better written as $3 at -e line 1. \1 better written as $1 at -e line 1. >OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/2 +0/2015<
Also note that you can avoid inflicting LTS upon yourself (and your eyes) by choosing a better regex delimiter:
c:\@Work\Perl\monks>perl -wMstrict -le "use strict; use warnings; ;; my $line = 'OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/ +30/2019,2015-06-20 00:00:00'; ;; $line =~ s{(\d{4})-(\d{2})-(\d{2})\s\d{2}:\d{2}:\d{2}}{$2/$3/$1}g; print qq{>$line<}; " >OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/2 +0/2015<
Please see perlre, perlretut and perlrequick.

Update: Your eyes will also thank you for some whitespace courtesy of the  /x modifier:
    $line =~ s{ (\d{4}) - (\d{2}) - (\d{2}) \s \d{2} : \d{2} : \d{2} }{$2/$3/$1}xg;
And is the  /g modifier really necessary?


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