Karan Verma
Karan Verma

Reputation: 1739

Split string by multiple delimiters

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?

Upvotes: 87

Views: 68831

Answers (6)

Arup Rakshit
Arup Rakshit

Reputation: 118299

Here is another one :

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

\w+ in Ruby regex matches one or more alphanumeric characters. It works by finding the first alphanumeric character, then checking the next character. If the next character is alphanumeric, it's included in the match. This process repeats until a non-alphanumeric character is found.

Upvotes: 25

user16452228
user16452228

Reputation:

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.

Upvotes: 1

James Stonehill
James Stonehill

Reputation: 1232

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.

Upvotes: 40

Cary Swoveland
Cary Swoveland

Reputation: 110755

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

Upvotes: 165

Benoît
Benoît

Reputation: 15010

Regex.

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

Upvotes: 66

Beartech
Beartech

Reputation: 6421

x = "one,two, three four" 

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

Upvotes: 4

Related Questions