To remove special characters from a string in Perl, you can use the s/// operator with a regular expression pattern.
In this example,
$str1
with a value of "Hello! World!"
.s///
operator with a regular expression pattern s/[^a-zA-Z0-9 ]//g
to replace all non-alphanumeric characters and spaces with an empty string globally.$str1
.$str1 = "Hello! World!";
$str1 =~ s/[^a-zA-Z0-9 ]//g;
print "Modified string: $str1\n";
Modified string: Hello World
In this example,
$str2
with a value of "apple@banana"
.s///
operator with a regular expression pattern s/[^a-zA-Z0-9 ]//g
to replace all non-alphanumeric characters and spaces with an empty string globally.$str2
.$str2 = "apple@banana";
$str2 =~ s/[^a-zA-Z0-9 ]//g;
print "Modified string: $str2\n";
Modified string: applebanana
In this tutorial, we learned How to Remove Special Characters from a String in Perl language with well detailed examples.