$text =~ s/(.*)$nbsp(.*)//g; # this is your problem #### $text = "test of  this # ? my 0923 5834t674"; $text =~ s{(.*?)(\ )?}{($x=$1)=~tr/ /./c; "$x$2"}ge; print "$text\n"; __OUTPUT__ .... ..  .... . . .. .... ........