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
2010-07-14 03:06:02
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
2010-07-14 03:25:09
`s.scan(/'(.+?)'|"(.+?)"|([^ ]+)/).flatten.compact` will exclude the quotes.
Wayne Conrad
2010-07-14 03:42:36
@Wayne, this doesn't work. It drops all quotes, including those surrounding a search phrase like 'dogs and cats'.
Gavin
2010-07-14 09:52:51
@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
2010-07-14 15:22:30
@Wayne, input: "cars 'cats and dogs' fish 'hammers'", output: ["cars", "cats and dogs", "fish", "hammers"]
Gavin
2010-07-15 00:43:08
@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
2010-07-15 00:51:04