如何测试Ruby字符串以匹配特定字符集(允许数字)。
即。我的设置为"AGHTM"
字符串"MT3G22AH"
有效
"TAR34"
,字符串R
无效。
答案 0 :(得分:5)
一个非常惯用的非正则表达式解决方案是使用String#count:
"MT3G22AH".count("^AGHTM0-9").zero? # => true
"TAR34".count("^AGHTM0-9").zero? # => false
答案 1 :(得分:4)
allowed = "AGHTM"
allowed = /\A[\d#{allowed}]+\z/i
"MT3G22AH" =~ allowed #⇒ truthy
"TAR34" =~ allowed #⇒ falsey
答案 2 :(得分:3)
一种可能性是删除所有允许的字符并检查结果字符串是否为空:
"MT3G22AH".delete("AGHTM0-9").empty?
#=> true
"TAR34".delete("AGHTM0-9").empty?
#=> false
对于短字符串,@ steenslag是最快的方法,其次是@Jesse和我的方法。
def mudasobwa(string)
allowed = 'AGHTM'
allowed = /\A[\d#{allowed}]+\z/i
string.match? allowed
end
def eric(string)
string.delete('AGHTM1-9').empty?
end
def meagar(string)
allowed = 'AGHTM0123456789'
string.chars.uniq.all? { |c| allowed.include?(c) }
end
def jesse(string)
string.count('^AGHTM0-9').zero?
end
def steenslag(string)
!string.match?(/[^AGHTM0-9]/)
end
require 'fruity'
n = 1
str1 = 'MT3G22AH' * n
str2 = 'TAR34' * n
compare do
_jesse { [jesse(str1), jesse(str2)] }
_eric { [eric(str1), eric(str2)] }
_mudasobwa { [mudasobwa(str1), mudasobwa(str2)] }
_meagar { [meagar(str1), meagar(str2)] }
_steenslag { [steenslag(str1), steenslag(str2)] }
end
输出:
Running each test 1024 times. Test will take about 2 seconds.
_steenslag is faster than _jesse by 2.2x ± 0.1
_jesse is faster than _eric by 8.000000000000007% ± 1.0%
_eric is faster than _meagar by 4.3x ± 0.1
_meagar is faster than _mudasobwa by 2.4x ± 0.1
对于更长的字符串(n=5000
),@ Jesse成为最快的方法。
Running each test 32 times. Test will take about 12 seconds.
_jesse is faster than _eric by 2.5x ± 0.01
_eric is faster than _mudasobwa by 4x ± 1.0
_mudasobwa is faster than _steenslag by 2x ± 0.1
_steenslag is faster than _meagar by 11x ± 0.1
答案 3 :(得分:1)
这似乎比以前所有基准测试都要快(@Eric Duminil)(红宝石2.4):
!string.match?(/[^AGHTM0-9]/)