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 than
shlexbut handles the most common use case just fine. It’s a convenient solution for a problem that comes up too often.
Update: Ruby 1.8.7 has added the shortcut methods
String#shellsplit. Very nice.