From: dblack@... Date: 2006-06-29T02:02:10+09:00 Subject: Re: remove all illegal chars form string ---2049402039-1735552145-1151500625=:26174 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1735552145-1151500625=:26174" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-1735552145-1151500625=:26174 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Wed, 28 Jun 2006, thomas coopman wrote: > Hi, > > Is there a simple way to remove all but the legal chars from a string. w= here 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|=A7" --> "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 b= etter. You can make a character class that contains what you want, and then get rid of everything that doesn't match it, using a negated character class: irb(main):004:0> str =3D "exam@p Le3|=C2=A7" =3D> "exam@p Le3|\302\247" irb(main):005:0> str.gsub(/[^A-Za-z0-9]/, '') =3D> "exampLe3" (The '3' disappeared in your example but assuming you want 0-9 it would actually remain.) David --=20 David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) See what the readers are saying about "Ruby for Rails"! http://www.rubypowerandlight.com/quotes ---2049402039-1735552145-1151500625=:26174-- ---2049402039-1735552145-1151500625=:26174--