Skip to content

Instantly share code, notes, and snippets.

@bensonk
Created August 12, 2011 21:07
Show Gist options
  • Select an option

  • Save bensonk/1143008 to your computer and use it in GitHub Desktop.

Select an option

Save bensonk/1143008 to your computer and use it in GitHub Desktop.
Tag parsing in ruby
#!/usr/bin/env ruby
tag_samples = [ 'foo bar', 'foo, bar, baz', 'foo "fun times" stuff', '"fail", "foo times"', "foo 'this is fun' bar" ]
tag_samples.each do |tag_str|
tags = tag_str.scan(/(\w+)|("([^"]+)")|('([^']+)')/).map { |a, _, b, _, c| (a or b or c).strip }
puts "#{tag_str}:"
puts tags
end
@livoffgrid
Copy link

I'd like it to work for single quotes as well as double quote.

'big balls' is equivalent to "big balls"

@bensonk
Copy link
Author

bensonk commented Aug 12, 2011

It does now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment