Split string by multiple delimiters

RubyStringSplit

Ruby Problem Overview


I want to split a string by whitespaces, , and ' using a single ruby command.

  1. word.split will split by white spaces;

  2. word.split(",") will split by ,;

  3. word.split("\'") will split by '.

How to do all three at once?

Ruby Solutions


Solution 1 - Ruby

word = "Now is the,time for'all good people"
word.split(/[\s,']/)
 => ["Now", "is", "the", "time", "for", "all", "good", "people"] 

Solution 2 - Ruby

Regex.

"a,b'c d".split /\s|'|,/
# => ["a", "b", "c", "d"]

Solution 3 - Ruby

You can use a combination of the split method and the Regexp.union method like so:

delimiters = [',', ' ', "'"]
word.split(Regexp.union(delimiters))
# => ["Now", "is", "the", "time", "for", "all", "good", "people"]

You can even use regex patters in the delimiters.

delimiters = [',', /\s/, "'"]
word.split(Regexp.union(delimiters))
# => ["Now", "is", "the", "time", "for", "all", "good", "people"]

This solution has the advantage of allowing totally dynamic delimiters or any length.

Solution 4 - Ruby

Here is another one :

word = "Now is the,time for'all good people"
word.scan(/\w+/)
# => ["Now", "is", "the", "time", "for", "all", "good", "people"]

Solution 5 - Ruby

x = "one,two, three four" 

new_array = x.gsub(/,|'/, " ").split

Solution 6 - Ruby

I know this is an old thread but I just happened to stumble on it and thought I would leave another answer. I personally like to avoid using regex, both because I find it hard to read and because its almost always slower than using other built in methods. So, instead of the aforementioned regex solutions, I would also consider using the following:

word.gsub(",", " ").gsub("'", " ").split

The first gsub replaces all occurrences of , with a space. The second gsub replaces all occurrences of ' with a space. This results in whitespace at all the desired locations. And then split with no argument simply splits on whitespace.

Its only slightly faster than some of the aforementioned solutions, but I do believe it is faster than any others that have been mentioned.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKaran VermaView Question on Stackoverflow
Solution 1 - RubyCary SwovelandView Answer on Stackoverflow
Solution 2 - RubyoldergodView Answer on Stackoverflow
Solution 3 - RubyJames StonehillView Answer on Stackoverflow
Solution 4 - RubyArup RakshitView Answer on Stackoverflow
Solution 5 - RubyBeartechView Answer on Stackoverflow
Solution 6 - RubyMichael BView Answer on Stackoverflow