my $string = "Hello ( World )."; $string =~ s/\(\s*//g; # remove opening bracket followed by spaces $string =~ s/\s*\)//g; # remove closing bracket preceded by space $string =~ s/\s/_/g; # replace spaces still left with underscores $string =~ s/\.$//; # remove ending period also