$_ = " yes \n \n \n they \n \n will \n\n\n"; s/^\s*?(?:\b(.*?)\s*?)?(\n)?$/$1$2/gm; print; __END__ % perl 443391.pl yes they will