in reply to separated quoted stings from unquoted strings
And just for fun heres another solution:use strict; use warnings; my $string = 'one two "three" four "five" six'; my (@quoted,@unquoted); my $length = 0; while ( $string =~ /\G\s*(?:("[^"]+")|([^"\s]+))(?:\s+)?/g ) { if ( $1 ) { push @quoted,$1 } else { push @unquoted,$2 } $length = pos $string; } unless ( length($string) == $length ) { print "Quotes did not match" } else { local $\ ="\n"; print q(@quoted:), join ",", @quoted; print q(@unquoted:),join ",",@unquoted; }
I doubt either solutions are the best solution. I think the best solution would depend on the strings and what you mean by a quoted string (can there be escaped quotes inside the string if so neither solution presented here will work). Well good luck, on your quest for knowledge.use strict; use warnings; my $string = ' one two "three" four "five" six'; my (@quoted,@unquoted); $\ ="\n"; 1 while ( $string =~ s/"([^"]+)"/push @quoted,$1;''/ge ); if ( $string =~ /"/ ) { print q(A " was found so failure) and exit; } push @unquoted, split " ", $string; print q(@quoted:), join ",", @quoted; print q(@unquoted:),join ",",@unquoted;
-enlil
|
|---|