On Wednesday 28 June 2006 14:09, thomas coopman wrote:
> 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"

You were right to think along the lines of gsub, a possible approach is:
"exam@p  Le3|.gsub(/[^[:alnum:]]/, '') -->"exampLe3"
This replaces all characters that are not in the :alnum: POSIX character class 
with a blank string.

If you just want alpha characters (not 0-9), then use [[:alpha:]] instead. 
Your example contradicted what you stated you were looking for.

The only thing to be wary of here is that Regexps such as /[[:alpha:]]/ may 
act differently depending on locale (no idea whether it has any effect in 
non-Onigurama Ruby 1.8.x), and certainly if executed on a Ruby compiled with 
the Onigurama regular expression engine (mine at least). With that in mind, 
the best way to get what you want may be to use /[a-zA-Z0-9]/ as your Regex.

As a side note, I didn't know that you could also do /[[:^alnum:]]/.

Hope this helps,
Alex