12

Take for instance, I have a string like this:

options = "Cake or pie, ice cream, or pudding"

I want to be able to split the string via or, ,, and , or.

The thing is, is that I have been able to do it, but only by parsing , and , or first, and then splitting each array item at or, flattening the resultant array afterwards as such:

options = options.split(/(?:\s?or\s)*([^,]+)(?:,\s*)*/).reject(&:empty?);
options.each_index {|index| options[index] = options[index].sub("?","").split(" or "); }

The resultant array is as such: ["Cake", "pie", "ice cream", "pudding"]

Is there a more efficient (or easier) way to split my string on those three delimiters?

3 Answers 3

15

What about the following:

options.gsub(/ or /i, ",").split(",").map(&:strip).reject(&:empty?)
  • replaces all delimiters but the ,
  • splits it at ,
  • trims each characters, since stuff like ice cream with a leading space might be left
  • removes all blank strings
2
  • 2
    Looks much easier to read, although there are two things: one, &:empty should be changed to &:empty?, and two, " or " could be changed to / or /i to accommodate uppercase 'OR' as well.
    – Mark
    Jun 1, 2011 at 21:16
  • Thanks about that - &:empty doesn't even work and I did indeed test it with &:empty?; and the regex is a handy addition as well.
    – mabako
    Jun 1, 2011 at 21:30
9

First of all, your method could be simplified a bit with Array#flatten:

>> options.split(',').map{|x|x.split 'or'}.flatten.map(&:strip).reject(&:empty?)
=> ["Cake", "pie", "ice cream", "pudding"]

I would prefer using a single regex:

>> options.split /\s*, or\s+|\s*,\s*|\s+or\s+/
=> ["Cake", "pie", "ice cream", "pudding"]

You can use | in a regex to give alternatives, and putting , or first guarantees that it won’t produce an empty item. Capturing the whitespace with the regex is probably best for efficiency, since you don’t have to scan the array again.

As Zabba points out, you may still want to reject empty items, prompting this solution:

>> options.split(/,|\sor\s/).map(&:strip).reject(&:empty?)
=> ["Cake", "pie", "ice cream", "pudding"]
4
  • 1
    What if the string started with an ,or for whatever reason? Your regex will then produce blank/empty strings.
    – Zabba
    Jun 1, 2011 at 20:43
  • 2
    Well, we do want to treat it as a delimiter. A delimiter at the beginning indicates an empty item. But I’ll address it.
    – Josh Lee
    Jun 1, 2011 at 20:48
  • There is one problem with the second solution, is that a word that is something like "smore's" produces ["sm","e's"]. it would probably be better to have the regexp be /,|\sor\s/. (And optionally use the 'i' mode to accept uppercase ` OR ` as well.)
    – Mark
    Jun 1, 2011 at 20:58
  • 2
    Better be careful "oranges" isn't in your list. You might want to switch from \s*or\s* to \s*\bor\b\s* like: /\s*,\s*\bor\b\s*|\s*,\s*|\s*\bor\b\s*/
    – tadman
    Jun 1, 2011 at 21:00
4

As "or" and "," does the same thing, the best approach is to tell the regex that multiple cases should be treated the same as a single case:

options = "Cake or pie, ice cream, or pudding"
regex = /(?:\s*(?:,|or)\s*)+/
options.split(regex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.