On Aug 2, 2007, at 2:26 PM, John J. wrote:
irb(main):002:0> “,.'abc123”.gsub(/[^[:alnum:]]/, ‘’)
How can do this while removing all non-space characters?
Found it. In Peter C.'s book! (I knew I bought that ebook for a
this is my final version:
@asset.permalink = @asset.name.downcase.gsub(' ', '_').gsub(/
downcase everything, turn spaces into underscores, then take all non-
alpha/non-numeric/non-underscore characters out.
sweet and simple.
For anyone else looking for it in the archives,
\w matches all alpha/numeric/underscore characters
\W matches everything else.
I could go a little further in the case of something like
title = ‘Untitled #234’
and do this:
title = title.downcase.gsub(’ ‘, ‘_’).gsub(’#’, ‘number’).gsub(/\W/, ‘’)
Nice and tidy. Could be a bit further refined to make sure a space is
prepended to ‘number’ if no space precedes ‘#’ and to make sure a
number actually follows ‘#’ in before going to all the trouble.
If I wanted hard to read, I could even try to squeeze it all into one
big RegEx, but the thing I’ve got above will suffice for now.