From: Jeff Everett Date: 2006-07-22T04:54:43+09:00 Subject: Re: Newbie Question: delete all non alphanumeric characters ------=_Part_2780_17421403.1153511678338 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I think \W is non-perl-word, so underscores won't be stripped. If you want those out too: irb(main):006:0> str = "The $re34& __q!?" => "The $re34& __q!?" irb(main):007:0> str.gsub( /\W/, '') => "There34__q" irb(main):008:0> str.gsub( /\W|_/, '') => "There34q" irb(main):009:0> Jeff On 7/21/06, Jim Cochrane wrote: > > On 2006-07-21, Theallnighter Theallnighter > wrote: > > Hi all, > > how can i delete all non alphanumeric characters in a string ? thanks > > > > I've also just started to learn Ruby, so thought I'd reply for the > practice - > Here's one solution: > > > ------------------------------------------------------------------------ > #!/usr/bin/ruby > > x = "There are 2007 beans and 15234 grains of rice in this bag." > puts x > x.gsub!(/\W/, '') > puts x > > ------------------------------------------------------------------------ > > output: > > There are 2007 beans and 15234 grains of rice in this bag. > Thereare2007beansand15234grainsofriceinthisbag > > -- > > > ------=_Part_2780_17421403.1153511678338--