从字符串中删除所有特殊字符 - ruby
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21446369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting all special characters from a string - ruby
提问by kwoskowicz
I was doing the challenges from pythonchallengewriting 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.
我正在接受pythonchallenge用 ruby 编写代码的挑战,特别是这个. 它在页面源中包含一个非常长的带有特殊字符的字符串。我试图找到一种方法来删除它们/检查字母字符。
I tried using scan method, but I think I might not use it properly. I also tried delete!like that:
我尝试使用扫描方法,但我认为我可能无法正确使用它。我也这样试过delete!:
a = "PAGE SOURCE CODE PASTED HERE"
a.delete! "!", "@" #and so on with special chars, does not work(?)
a
How can I do that?
我怎样才能做到这一点?
Thanks
谢谢
回答by Alok Anand
You can do this
你可以这样做
a.gsub!(/[^0-9A-Za-z]/, '')
回答by arieljuod
try with gsub
尝试使用 gsub
a.gsub!(/[!@%&"]/,'')
try the regexp on rubular.com
尝试 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 ]/,'')
回答by dee-see
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.
当您为 提供多个参数时string#delete,删除的是这些参数的交集。a.delete! "!", "@"删除组的交点!和@这意味着没有将被删除并且该方法返回nil。
What you wanted to do is a.delete! "!@"with the characters to delete passed as a single string.
您想要做的是a.delete! "!@"将要删除的字符作为单个字符串传递。
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 ".
由于挑战是要求清理混乱并在其中查找消息,因此我会使用白名单而不是删除特殊字符。删除方法接受与范围-,并与否定^(类似正则表达式),所以你可以做这样的事情:a.delete! "^A-Za-z "。
You could also use regular expressions as shown by @arieljuod.
您还可以使用@arieljuod 所示的正则表达式。
回答by Pradeep
gsubis one of the most used Ruby methods in the wild.
gsub是最常用的 Ruby 方法之一。
specialname="Hello!#$@"
cleanedname = specialname.gsub(/[^a-zA-Z0-9\-]/,"")
回答by AGS
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
回答by ThaDick
I think a.gsub(/[^A-Za-z0-9 ]/, '')works better in this case. Otherwise, if you have a sentence, which typically shouldstart with a capital letter, you will lose your capital letter. You would also lose any 1337 speak, or other possible crypts within the text.
我认为a.gsub(/[^A-Za-z0-9 ]/, '')在这种情况下效果更好。否则,如果您有一个句子,通常应该以大写字母开头,您将丢失大写字母。您还将丢失1337 speak文本中的任何或其他可能的隐窝。
Case in point:
案例:
phrase = "Joe can't tell between 'large' and large."
=> "Joe can't tell between 'large' and large."
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-z ]/, '')
=> "oe cant tell between large and large"
phrase.gsub(/[^A-Za-z0-9 ]/, '')
=> "Joe 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 = "W3 a11 f10a7 d0wn h3r3!"
phrase2.gsub(/[^a-z ]/, '')
=> " a fa dwn hr"
phrase2.gsub(/[^A-Za-z0-9 ]/, '')
=> "W3 a11 f10a7 d0wn h3r3"
phrase2.gsub(/[^A-Za-z0-9 ]/, '')
=> "W3 a11 f10a7 d0wn h3r3"

