in reply to substitution not working correctly

Alternatively, remove the space afterwards.
sub trim { for (my $s = $_[0]) { s/^\s+//; s/\s+$//; return $_; } } $string =~ s/##([^#]+)##/##$langhash{trim($1)}##/;