Re: a regex question ..



* Geoff Cox schrieb:

> Hello
>
> 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!

Yes, it isn't. Try to use s/// instead.

my $name = 'aaa2bbb3';
( my $newname = $name ) =~ s/(\d)/0$1/g;
# results in: $newname = 'aaa02bbb03'

Be aware of the /g-modifier at the end of s///. Read `perldoc perlop` to
learn more about this substitution operator.

regards,
fabian
.



Relevant Pages

  • a regex question ..
    ... I am trying to rename a list of files by changing any single digits in ... Geoff ... Prev by Date: ...
    (comp.lang.perl.misc)
  • Re: Return Consecutive Values - Pairs
    ... couple of Input cells with single digits, so they were tweaked with LEFT(B$1, ... The Formula remains as your Posting for all double digit values. ... Prev by Date: ...
    (microsoft.public.excel.worksheet.functions)