use re 'debug'; my $inputB = "X".(" \t" x 10000)."B"; $inputB =~ s/[\s\u200c]*$/ A/;