my $string = ' this is a string'; my $rep; $string =~ s/(^\s+)/$rep.=':' for(1..length($1)); "$rep"/eg; print $string;