# Splitting a string into words using multiple possible separa

**URL:** <https://www.ruby-forum.com/t/splitting-a-string-into-words-using-multiple-possible-separa/92553>\
**Category:** Ruby\
**Created:** [April 20, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/splitting-a-string-into-words-using-multiple-possible-separa/92553 "2007-04-20T16:47:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 20, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/splitting-a-string-into-words-using-multiple-possible-separa/92553/1 "2007-04-20T16:47:22Z")

</div>

Hi,

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  
array

e.g.,

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)?

tia

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 20, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/splitting-a-string-into-words-using-multiple-possible-separa/92553/2 "2007-04-20T17:06:24Z")

</div>

Perhap:

> > string = 'my uncle, jumped over;

the moon’

a = string.scan(/\b\w+/)  
[“my”, “uncle”, “jumped”, “over”, “the”, “moon”]

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 20, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/splitting-a-string-into-words-using-multiple-possible-separa/92553/3 "2007-04-20T22:11:02Z")

</div>

string.split(/[^a-zA-Z0-9-]/)

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 20, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/splitting-a-string-into-words-using-multiple-possible-separa/92553/4 "2007-04-20T17:31:27Z")

</div>

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”]

Or

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

robert
