Split string into multiple delimiters

I want to split the string with spaces,, and ' with a single ruby โ€‹โ€‹command.

  • word.split will be separated by spaces;

  • word.split(",") will be broken into word.split(",")

  • word.split("\'") will split into ' .

How to do all three at the same time?

+42
string split ruby
Oct 22 '13 at 4:59 on
source share
4 answers
 word = "Now is the,time for'all good people" word.split(/[\s,']/) => ["Now", "is", "the", "time", "for", "all", "good", "people"] 
+86
Oct 22 '13 at 5:06 on
source share

Regex.

 "a,b'c d".split /\s|'|,/ # => ["a", "b", "c", "d"] 
+29
Oct 22 '13 at 5:04 on
source share

Here is another one:

 word = "Now is the,time for'all good people" word.scan(/\w+/) # => ["Now", "is", "the", "time", "for", "all", "good", "people"] 
+15
Oct 22 '13 at 6:33
source share
 x = "one,two, three four" new_array = x.gsub(/,|'/, " ").split 
+3
Oct 22 '13 at 5:08 on
source share



All Articles