[英]How to remove all non-digits from a string in ruby?
Users input numbers in the following forms: 用户以下列形式输入数字:
1-800-432-4567
800-432-4567
800.432.4566
(800)432.4567
+1(800)-432-4567
800 432 4567
I want all of these to be turned into a stripped version without the special characters like 18004324567
. 我希望所有这些都变成一个剥离版本,没有像
18004324567
这样的特殊字符。 The data come in the form of a String
, so string checking isn't required. 数据以
String
的形式出现,因此不需要进行字符串检查。
My method is as below: 我的方法如下:
def canonical_form number
a = remove_whitespaces number #to clear all whitespaces in between
a.gsub(/[()-+.]/,'')
end
def remove_whitespaces number
number.gsub(/\s+/,'') #removes all whitespaces
end
Is there a better way to do this? 有一个更好的方法吗? Can the white space check with the regular expression in
canonical_form
method be performed without having an extra method for white spaces? 可以使用
canonical_form
方法中的canonical_form
则表达式检查空格,而无需额外的空格方法吗? How can this be refactored or done in a neater way? 如何以更简洁的方式对其进行重构或完成?
If the first argument of the tr
method of String starts with ^
, then it denotes all characters except those listed. 如果String的
tr
方法的第一个参数以^
开头,那么它表示除列出的所有字符之外的所有字符。
def canonical_form str
str.tr('^0-9', '')
end
Several solutions above - I benchmarked a few in case anyone is interested: 上面的几个解决方案 - 我对一些人感兴趣的基准测试:
str = "1-800-432-4567"
Benchmark.ms { 10000.times{str.scan(/\d/).join} }
#=> 69.4419999490492
Benchmark.ms { 10000.times{str.delete('^0-9')} }
#=> 7.574999995995313
Benchmark.ms { 10000.times{str.tr('^0-9', '')} }
#=> 7.642999989911914
Benchmark.ms { 10000.times{str.gsub(/\D+/, '')} }
#=> 28.084999998100102
Instead of removing special characters, you can looks for all digits. 您可以查找所有数字,而不是删除特殊字符。 Something like:
就像是:
str = "1-800-432-4567"
str.scan(/\d/).join
#=> "18004324567"
str = "(800)432.4567"
str.scan(/\d/).join
#=> "8004324567"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.