Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Parse An Argument List Containing A Quoted String

DZone's Guide to

Parse An Argument List Containing A Quoted String

·
Free Resource
The following example parses a string containing a quoted string:

s = %q(passed "abc def" etert)
s.gsub!(/\".[^\|\'"]+["']/) {|x| x.gsub(/\s/,'%20')}
a = s.split(/\s/).map {|x| x.gsub(/%20/,' ')}
#=> ["passed", "\"abc def\"", "etert"]


*update: 23-Mar-2010 @ 1:50pm*
Here's how to remove the quotes from the final result:

a = s.split(/\s/).map {|x| x.gsub(/%20/,' ').sub(/\"(.*)\"/,'\1')}
#=> ["passed", "abc def", "etert"]
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}