Reputation: 1739
I want to split a string by whitespaces, ,
and '
using a single ruby command.
word.split
will split by white spaces;
word.split(",")
will split by ,
;
word.split("\'")
will split by '
.
How to do all three at once?
Upvotes: 87
Views: 68831
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
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
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
Reputation: 110755
word = "Now is the,time for'all good people"
word.split(/[\s,']/)
=> ["Now", "is", "the", "time", "for", "all", "good", "people"]
Upvotes: 165