>perl -wMstrict -le "my @first_sentence = qw{ i am 2 ( but ) . }; printf qq{'$_' } for @first_sentence; print ''; ;; my $ns = qr{ [().] }xms; ;; my $sentence = join q{ }, @first_sentence; print qq{'$sentence'}; ;; $sentence =~ s{ [ ] (?= $ns) | (?<= $ns) [ ] }''xmsg; print qq{'$sentence'}; ;; ;; use List::Util qw(reduce); ;; $sentence = ''; $sentence = reduce { ($a =~ $ns || $b =~ $ns) ? qq{$a$b} : qq{$a $b} } @first_sentence ; print qq{'$sentence'} " 'i' 'am' '2' '(' 'but' ')' '.' 'i am 2 ( but ) .' 'i am 2(but).' 'i am 2(but).'