Update:Enlil++ points out that the original solution below isn't. I completely missed capturing the first unquoted word in the example string, which translates to missing any and all unquoted strings that are not followed by a quoted string or the end-of-string! Whoops.
The addition of * at the end of the second regex fixes the problem (I think?).
$s = 'one seven eight two "three" four "five" "nine" ten eleven six tw +elve'; @q = $S =~ m[" ( [^"]+ ) "]gx; print @q three five nine @u = $S =~ m[(\S+)\s* (?: (?: " [^"]+ " \s*) | $ )* ]gx; print @u one seven eight two four ten eleven six twelve
Provided your quoted strings don't contain embedded quotes, this should work once you've checked for balanced quotes.
$string = 'one two "three" four "five" six';
@quoted = $string =~ m[" ( ^"+ ) "]gx;
@unquoted = $string =~ m[(\S+)\s* (?: (?: " ^"+ " \s*) | $ )]gx;
print @quoted;
print @unquoted;
three five
two four six
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
In reply to Re: separated quoted stings from unquoted strings
by BrowserUk
in thread separated quoted stings from unquoted strings
by Cody Pendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |