$line =~ s/ \[ .*? \] //gx; # remove strings enclosed by "[..]" $line =~ s/ \s+ (\S) / $1/gx; # normalize line-internal whitespace