Search code examples
perl5.8

How to replace all characters in a string with '*' in perl


How to get a regular expression to replace all characters in a string in perl with *? The string has some utf-8 or iso-8859-1 characters also. I tried with "s/\w/*/g". But it did not replace utf-8 or iso-8859-1 characters.

my $value="hellö";
print "$value\n";
$value =~ s/\w/*/g;
print "after replacing $value\n"; //It prints ****ö.

I expect all characters should be replaced with * i.e hellö should be replaced with *****.

Please note, few special characters like -,_,\,/ etc should be skipped.


Solution

  • If you want to skip just a few characters, you can always do something along the lines of

     s/[^, \/\\\-]/*/g;