s/^\s+//; #trim leading whitespace s/\s+$//; #trim trailing whitespace s/(\S+)\s{2,}/"$1 "/gie; #replace more then one space with one space s/\s{2,}(\S+)/" $1"/gie; #replace more then one space with one space