tags:

views:

58

answers:

1

Given a search string "cars 'cats and dogs' fish 'hammers'", what's the best regex for capturing all search terms. It should support single and double quotes. A Ruby friendly answer if possible.

+4  A: 

Using String#scan:

irb> "cars 'cats and dogs' fish 'hammers'".scan /'.+?'|".+?"|[^ ]+/
  => ["cars", "'cats and dogs'", "fish", "'hammers'"]

Oh, and to get rid of surrounding quotes in the result:

irb> ["cars", "'cats and dogs'", "fish", "'hammers'"].map { |s| s.gsub /^['"]|['"]$/, '' }
  => ["cars", "cats and dogs", "fish", "hammers"]
yjerem
That works and I can use it, but I'd like to do the job without the map. I have a regex that almost does the trick but it's capturing an extra quote on quoted phrases: /("?)\s*\b(\S[^\1]*?)\b\s*\1/
Gavin
`s.scan(/'(.+?)'|"(.+?)"|([^ ]+)/).flatten.compact` will exclude the quotes.
Wayne Conrad
@Wayne, this doesn't work. It drops all quotes, including those surrounding a search phrase like 'dogs and cats'.
Gavin
@Gavin, From your previous comment, that's what I thought you wanted. Sorry I misunderstood. Your question has sample input; if it had the output that should result from that input, that would be good.
Wayne Conrad
@Wayne, input: "cars 'cats and dogs' fish 'hammers'", output: ["cars", "cats and dogs", "fish", "hammers"]
Gavin
@Gavin, Thanks. That's exactly the output that my modification of @yjerem's code produces. I'm using MRI 1.8.7. Are you sure that the code is broken?
Wayne Conrad