T
Theallnighter Theallnighter
Hi all,
how can i delete all non alphanumeric characters in a string ? thanks
how can i delete all non alphanumeric characters in a string ? thanks
Hi all,
how can i delete all non alphanumeric characters in a string ? thanks
That deletes all alphanumeric. To delete all non-alphanumeric:Logan said:Hi all,
how can i delete all non alphanumeric characters in a string ? thanks
string.gsub(/[0-9a-z]+/i, '')
That deletes all alphanumeric. To delete all non-alphanumeric:Logan said:Hi all,
how can i delete all non alphanumeric characters in a string ?
thanks
string.gsub(/[0-9a-z]+/i, '')
string.gsub(/[^0-9a-z]/i, '')
--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
www.helmetstohardhats.org
Hi all,
how can i delete all non alphanumeric characters in a string ? thanks
I've also just started to learn Ruby, so thought I'd reply for the
practice -
Here's one solution:
----------------------------------------------------------------------
--
#!/usr/bin/ruby
x = "There are 2007 beans and 15234 grains of rice in this bag."
puts x
x.gsub!(/\W/, '')
puts x
----------------------------------------------------------------------
--
output:
There are 2007 beans and 15234 grains of rice in this bag.
Thereare2007beansand15234grainsofriceinthisbag
Well the only "problem" with that is
x = '\w includes_under_scores_too'
Tom said:That deletes all alphanumeric. To delete all non-alphanumeric:Logan said:Hi all,
how can i delete all non alphanumeric characters in a string ? thanks
string.gsub(/[0-9a-z]+/i, '')
string.gsub(/[^0-9a-z]/i, '')
--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
www.helmetstohardhats.org
Well the only "problem" with that is
x = '\w includes_under_scores_too'
Woah! Thanks for pointing that out. It looks like
http://www.ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/regexp.html
has a bug:
\w letter or digit; same as [0-9A-Za-z]
It's missing a _.
Here's a fixed version:
#!/usr/bin/ruby
x = "There are 2007 beans_and 15234 grains of rice in this bag."
puts x
x.gsub!(/\W/, '')
puts x
x.gsub!(/\W|_/, '')
puts "fixed:"
puts x
for fun, I started irb, then typed
"567576hgjhgjh&**)".gsub(/^[0-9a-z]/i, '')
It returned
67576hgjhgjh&**)
"567576hgjhgjh&**)".gsub(/^[0-9a-z]/i, '')
It returned
67576hgjhgjh&**)
for fun, I started irb, then typed
"567576hgjhgjh&**)".gsub(/^[0-9a-z]/i, '')
It returned
67576hgjhgjh&**)
The carat goes inside the brackets (it inverses the character class)
Jeremy said:And it should look like this:
"567576hgjhgjh&**)".sub(/[^0-9a-zA-Z]+/i, '')
Note the +
Jeremy said:And it should look like this:
"567576hgjhgjh&**)".sub(/[^0-9a-zA-Z]+/i, '')
Note the +
#sub only does one replacement; adding a + will replace one chunk
of non-alphas, but not any others in the string.
Jeremy said:And it should look like this:
"567576hgjhgjh&**)".sub(/[^0-9a-zA-Z]+/i, '')
Note the +
#sub only does one replacement; adding a + will replace one chunk
of non-alphas, but not any others in the string.
typo, sorry.
Hi all,
how can i delete all non alphanumeric characters in a string ? thanks
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.