Parsing Quoted Strings in Ruby
-
Python has a nice module in the standard library called shlex that parses strings as a Unix shell would. Here’s a Python interpreter session demonstrating its usage:
>>> import shlex >>> shlex.split('foo "bar baz" qux') ['foo', 'bar baz', 'qux']
It’s useful for creating your own mini-languages or external DSLs that need to parse quoted strings like the one shown above.
We recently built an inventory tracking application in Ruby that has a user interface for selecting search filters. To expose the same search capabilities as a web service, we created a simple query language. I was looking for an
shlexequivalent in Ruby.It turns out that the Ruby Standard Library has a module called Shellwords:
>> require 'shellwords' => true >> Shellwords::shellwords('foo "bar baz" qux') => ["foo", "bar baz", "qux"]
Shellwordsis a little less capable thanshlexbut handles the most common use case just fine. It’s a convenient solution for a problem that comes up too often.

3 comments
comment by Henrik N 5 May 08
Note that
>> Shellwords.shellwords %{”a”"b”}
=> ["ab"]
though. That makes sense in some contexts (shells) but not in others (Flickr-style tag parsing) where you might be tempted to use this lib.
See my post http://henrik.nyh.se/2008/03/flickr-style-tag-splitting-in-ruby and the comments for more.
comment by Mike Naberezny 5 May 08
Good catch. Interestingly, tagging a photo on Flickr itself with your string of
"a""b"also results in one tag,ab.comment by Henrik N 6 May 08
Mike: Ooh, full circle. Cool, didn’t try that.
Post a comment