To keep only alphabets in a string in Perl, you can use regular expressions or the 'tr' function to remove non-alphabetic characters.
In this example,
$str1
with a value of "Hello123 World"
.tr
function with a regular expression /[^A-Za-z]//d
to remove non-alphabetic characters.$result1
.$result1
.$str1 = "Hello123 World";
$str1 =~ tr/[^A-Za-z]//d;
print "Modified string: $str1\n";
Modified string: HelloWorld
In this example,
$str2
with a value of "apple@banana"
.tr
function with a regular expression /[^A-Za-z]//d
to remove non-alphabetic characters.$result2
.$result2
.$str2 = "apple@banana";
$str2 =~ tr/[^A-Za-z]//d;
print "Modified string: $str2\n";
Modified string: applebanana
In this tutorial, we learned How to keep only Alphabets in a String in Perl language with well detailed examples.