From: Jim Cochrane Date: 2006-07-22T05:06:51+09:00 Subject: Re: Newbie Question: delete all non alphanumeric characters On 2006-07-21, Logan Capaldo wrote: > > On Jul 21, 2006, at 3:40 PM, Jim Cochrane wrote: > >> On 2006-07-21, Theallnighter Theallnighter >> wrote: >>> Hi all, >>> how can i delete all non alphanumeric characters in a string ? thanks >>> >> ... >> #!/usr/bin/ruby >> >> x = "There are 2007 beans and 15234 grains of rice in this bag." >> puts x >> x.gsub!(/\W/, '') >> puts x >> ... >> >> > > Well the only "problem" with that is > > x = '\w includes_under_scores_too' > Woah! Thanks for pointing that out. It looks like http://www.ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/regexp.html has a bug: \w letter or digit; same as [0-9A-Za-z] It's missing a _. Here's a fixed version: #!/usr/bin/ruby x = "There are 2007 beans_and 15234 grains of rice in this bag." puts x x.gsub!(/\W/, '') puts x x.gsub!(/\W|_/, '') puts "fixed:" puts x