From: "Alexandru E. Ungur" Date: 2006-06-29T02:10:00+09:00 Subject: Re: remove all illegal chars form string >>> sender: "thomas coopman" date: "Wed, Jun 28, 2006 at 10:09:35PM +0900" << Hi, Hi, > Is there a simple way to remove all but the legal chars from a string. > where the legal chars are for example: a-z A-Z 0-9 > So everything should be removed from the string but these characters. > "exam@p Le3|�" --> "exampLe" Taking your example, if legal chars are a-z A-Z 0-9 then the output of: "exam@p Le3|�" should be: "exampLe3" and not "exampLe"... > I don't know very much about regular expressions, so I don't know if > it's possible with sub or gsub. My first Idea was to loop over the > string and check every character but I wondered if there is something > more simple or better. Yes, this is why regexps were invented :) # irb irb(main):001:0> "exam@p Le3|�".gsub(/[^a-zA-Z0-9]/,'') => "exampLe3" > Thanks You're welcome, Alex