Forum: Ruby splitting a string into words using multiple possible separa

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B8e48899be4fbcc29b4b9bbae87a965a?d=identicon&s=25 Paul Danese (Guest)
on 2007-04-20 16:47
(Received via mailing list)

I'm using ruby 1.8.5

i have an input string whose 'words' are separated by whitespace and/or
commas and/or semicolons

I want to remove the separators (,;\s) and place each 'word' into an


this string => 'my uncle, jumped    over;

the moon'

would be converted into an array of elements ['my', 'uncle', 'jumped',
'over', 'the', 'moon']

currently, i'm doing this (in this example, rx_input holds the string
and rx_numbers is the array):

    rx_input.gsub!(/[,;\s]/, ' ')

    rx_input.squeeze!(' ')

    rx_numbers = rx_input.split(' ')

is there a simpler /more elegant/ way of doing this (accounting for the
possibility of double-spaces, etc)?

6a8333cdf53797dd314bb66eb0797c14?d=identicon&s=25 Doan, Alex (Guest)
on 2007-04-20 17:06
(Received via mailing list)
>> string = 'my uncle, jumped    over;

the moon'

a = string.scan(/\b\w+/)
["my", "uncle", "jumped", "over", "the", "moon"]
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-04-20 17:31
(Received via mailing list)
On 20.04.2007 17:05, Doan, Alex wrote:
> Perhap:
>>> string = 'my uncle, jumped    over;
> the moon'
> a = string.scan(/\b\w+/)
> ["my", "uncle", "jumped", "over", "the", "moon"]


irb(main):001:0> string = 'my uncle, jumped    over; the moon'
=> "my uncle, jumped    over; the moon"
irb(main):002:0> string.scan /\w+/
=> ["my", "uncle", "jumped", "over", "the", "moon"]
irb(main):003:0> string.split /\W+/
=> ["my", "uncle", "jumped", "over", "the", "moon"]

Kind regards

8b22ad683842861ba91f68d5d32954db?d=identicon&s=25 Jason (Guest)
on 2007-04-20 22:11
(Received via mailing list)
This topic is locked and can not be replied to.