#!/usr/bin/perl -w use strict; my $string = "a 'b c d' e f 'g h'"; my $tmp=''; my @result = (); for (split /\s+/, $string) { if (/^'/) { $tmp = $_; next; } elsif (/'$/) { push @result, $tmp." $_"; $tmp=''; next; } elsif($tmp) { $tmp.=" $_"; } else { push @result,$_; } } print join "\n", @result;