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

1.7k views Asked by At

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.

2

There are 2 answers

0
Don Hosek On BEST ANSWER

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

 s/[^, \/\\\-]/*/g;
5
Steve Friedl On

To replace all the characters in a string? The \w is for matching word characters, but using just a dot should match all characters: s/./*/g