Re: a regex question ..



Geoff Cox wrote:

I am trying to rename a list of files by changing any single digits in the names to a zero and the digit, eg 7 -> 07.

Problem is there may be 2 instances in the name and the code below
only works for the first single digit - how can I deal with both
instances?


if ($name =~ /^(.*?)\-(\d{1})\D(.*?)$/ ) {
my $num = $2;
rename ($name, "new-" . $1 . "-" . "0" . $num . "-" . $3);


I have tried using tr/// but cannot see how to do it.

My guess would be that the above is not the best way to go about this!


( my $newname = "new-$name" ) =~ s/(?<!\d)(\d)(?!\d)/0$1/g;

if ( -e $newname ) {
    warn "Cannot rename $name because $newname exists.\n";
    }
else {
    rename $name, $newname or warn "Cannot rename $name to $newname: $!";
    }



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: a regex question ..
    ... Geoff Cox wrote: ... means that there must not be a single digit ahead of the the single ... use Perl; ...
    (comp.lang.perl.misc)
  • Re: multiple lines / success or failure?!
    ... Geoff Cox wrote: ... >>look that message up in the reference manual (perldiag)? ... The best resource available for Perl is the standard documentation ...
    (comp.lang.perl.misc)