Deleting all special characters from a string - ruby

RubySpecial Characters

Ruby Problem Overview


I was doing the challenges from pythonchallenge writing code in ruby, specifically this one. It contains a really long string in page source with special characters. I was trying to find a way to delete them/check for the alphabetical chars.

I tried using scan method, but I think I might not use it properly. I also tried delete! like that:

    a = "PAGE SOURCE CODE PASTED HERE"
    a.delete! "!", "@"  #and so on with special chars, does not work(?) 
    a

How can I do that?

Thanks

Ruby Solutions


Solution 1 - Ruby

You can do this

a.gsub!(/[^0-9A-Za-z]/, '')

Solution 2 - Ruby

try with gsub

a.gsub!(/[!@%&"]/,'')

try the regexp on rubular.com

if you want something more general you can have a string with valid chars and remove what's not in there:

a.gsub!(/[^abcdefghijklmnopqrstuvwxyz ]/,'')

Solution 3 - Ruby

When you give multiple arguments to string#delete, it's the intersection of those arguments that is deleted. a.delete! "!", "@" deletes the intersections of the sets ! and @ which means that nothing will be deleted and the method returns nil.

What you wanted to do is a.delete! "!@" with the characters to delete passed as a single string.

Since the challenge is asking to clean up the mess and find a message in it, I would go with a whitelist instead of deleting special characters. The delete method accepts ranges with - and negations with ^ (similar to a regex) so you can do something like this: a.delete! "^A-Za-z ".

You could also use regular expressions as shown by @arieljuod.

Solution 4 - Ruby

gsub is one of the most used Ruby methods in the wild.

specialname="Hello!#$@"
cleanedname = specialname.gsub(/[^a-zA-Z0-9\-]/,"") 

Solution 5 - Ruby

I think a.gsub(/[^A-Za-z0-9 ]/, '') works better in this case. Otherwise, if you have a sentence, which typically should start with a capital letter, you will lose your capital letter. You would also lose any 1337 speak, or other possible crypts within the text.

Case in point:

phrase = "Joe can't tell between 'large' and large." => "Joe can't tell between 'large' and large."

phrase.gsub(/[^a-z ]/, '') => "oe cant tell between large and large"

phrase.gsub(/[^A-Za-z0-9 ]/, '') => "Joe cant tell between large and large"

phrase2 = "W3 a11 f10a7 d0wn h3r3!" phrase2.gsub(/[^a-z ]/, '') => " a fa dwn hr"

phrase2.gsub(/[^A-Za-z0-9 ]/, '') => "W3 a11 f10a7 d0wn h3r3"

Solution 6 - Ruby

If you don't want to change the original string - i.e. to solve the challenge.

str.each_char do |letter|
  if letter =~ /[a-z]/  
    p letter    
  end  
end  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkwoskowiczView Question on Stackoverflow
Solution 1 - RubyAlok AnandView Answer on Stackoverflow
Solution 2 - RubyarieljuodView Answer on Stackoverflow
Solution 3 - Rubydee-seeView Answer on Stackoverflow
Solution 4 - RubyPradeepView Answer on Stackoverflow
Solution 5 - RubyThaDickView Answer on Stackoverflow
Solution 6 - RubyAGSView Answer on Stackoverflow