$string =~ s/_/ /g; # replace each _ with a space $string =~ s/_+/ /g; # replace each run of _ with a single space