my $foo = "0:0A:0C:B:B8:F"; $foo =~ s/\A([0-9A-F]){1}:/0$&/g; #take care of the start $foo =~ s/:([0-9A-F]{1}):/:0$1:/g; #take care of the middle $foo =~ s/:([0-9A-F]{1})\Z/:0$1/g; #take care of the end $foo =~ s/\://g; #remove the colon