[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: splitting a string into words using multiple possible separators

Doan, Alex

4/20/2007 3:06:00 PM

Perhap:
>> string = 'my uncle, jumped over;

the moon'

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

-----Original Message-----
From: Paul Danese [mailto:pdanese@Rib-x.com]
Sent: Friday, April 20, 2007 10:46 AM
To: ruby-talk ML
Subject: splitting a string into words using multiple possible
separators

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


2 Answers

Robert Klemme

4/20/2007 3:27:00 PM

0

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

Jason

4/20/2007 8:06:00 PM

0

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

Robert Klemme wrote:
> 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