$ # Code (1-liner, expanded) and example output, $ # handles both '###-##-####' and '#########' formats $ perl -le ' my @c = qw/ 123-45-6789 123456789 / ; foreach my $d ( @c ) { my $e = $d; $e =~ s/^(\d+)(-?)(\d+)(-?)(\d{4})$/ q{#} x length($1) . $2 . q{#} x length($3) . $4 . $5/egimsx; print $e; }' ###-##-6789 #####6789