From: Mat Schaffer Date: 2006-06-29T02:10:29+09:00 Subject: Re: remove all illegal chars form string On Jun 28, 2006, at 9:09 AM, thomas coopman wrote: > 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" > > 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. > > Thanks The regexp for such a thing would be: "exam@p Le3|�".gsub(/[^a-zA-Z0-9]/, "") => "exampLe3" (you listed 3 as a legal character in the above email. /[^a-zA-Z]/ would remove numbers as well) Probably about time to learn some regular expressions. Have a look at http://www.regular-expressions.info/tutorial.html You'll really start to like them once you learn even just the basic matching ideas. -Mat