用于检查 ruby 中的字母数字字符串的正则表达式
Regex to check alphanumeric string in ruby
我正在尝试验证 ruby 中的字符串。
任何包含空格、下划线或任何特殊字符的字符串都应该无法通过验证。
有效字符串应仅包含字符 a-zA-Z0-9
我的代码看起来像。
def validate(string)
regex ="/[^a-zA-Z0-9]$/
if(string =~ regex)
return "true"
else
return "false"
end
我遇到错误:
类型错误:类型不匹配:给定的字符串。
谁能告诉我正确的做法是什么?
您可以只检查字符串中是否存在特殊字符。
def validate str
chars = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
str.chars.detect {|ch| !chars.include?(ch)}.nil?
end
结果:
irb(main):005:0> validate "hello"
=> true
irb(main):006:0> validate "_90 "
=> false
如果您正在验证一行:
def validate(string)
!string.match(/\A[a-zA-Z0-9]*\z/).nil?
end
每个都不需要 return。
类似于@rohit89:
VALID_CHARS = [*?a..?z, *?A..?Z, *'0'..'9']
#=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
# "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
# "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
# "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
# "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
def all_valid_chars?(str)
a = str.chars
a == a & VALID_CHARS
end
all_valid_chars?('a9Z3') #=> true
all_valid_chars?('a9 Z3') #=> false
没有正则表达式:
def validate(str)
str.count("^a-zA-Z0-9").zero? # ^ means "not"
end
上面的答案很好,但仅供参考,您的错误消息是因为您用双引号 "
启动了正则表达式。您会注意到您的方法中有奇数 (5) 个双引号。
此外,您可能希望 return true 和 false 作为值而不是引用字符串。
def alpha_numeric?(char)
if((char =~ /[[:alpha:]]) || (char =~ [[:digits:]]))
true
else
false
end
end
或
def alpha_numeric?(char)
if(char =~ /[[:alnum:]])
true
else
false
end
end
我们正在使用匹配字母和数字的正则表达式:
上面的 [[:alpha:]] ,[[:digit:]] 和 [[:alnum:]] 是 POSIX 括号表达式,它们的优点是匹配Unicode字符类别。希望这有帮助。
查看下面的 link 以获得更多选项:
Ruby: How to find out if a character is a letter or a digit?
在 Ruby 2.4+.
中使用 .match?
Ruby 2.4 引入了一种方便的布尔返回 .match?
方法。
对于你的情况,我会这样做:
# Checks for any characters other than letters and numbers.
# Returns true if there are none. Returns false if there are one or more.
#
def valid?( string )
!string.match?( /[^a-zA-Z0-9]/ ) # NOTE: ^ inside [] set turns it into a negated set.
end
类似于@steenslag 已经提到的非常有效的 regex-ish 方法,而且几乎一样快:
str.tr("a-zA-Z0-9", "").length.zero?
或
str.tr("a-zA-Z0-9", "") == 0
使用 tr
的一个好处是您还可以选择使用相同的基本公式分析结果:
str = "ABCxyz*123$"
rejected_chars = str.tr("a-zA-Z0-9", "")
#=> *$
is_valid = rejected_chars.length.zero?
#=> false
我正在尝试验证 ruby 中的字符串。 任何包含空格、下划线或任何特殊字符的字符串都应该无法通过验证。 有效字符串应仅包含字符 a-zA-Z0-9 我的代码看起来像。
def validate(string)
regex ="/[^a-zA-Z0-9]$/
if(string =~ regex)
return "true"
else
return "false"
end
我遇到错误: 类型错误:类型不匹配:给定的字符串。
谁能告诉我正确的做法是什么?
您可以只检查字符串中是否存在特殊字符。
def validate str
chars = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
str.chars.detect {|ch| !chars.include?(ch)}.nil?
end
结果:
irb(main):005:0> validate "hello"
=> true
irb(main):006:0> validate "_90 "
=> false
如果您正在验证一行:
def validate(string)
!string.match(/\A[a-zA-Z0-9]*\z/).nil?
end
每个都不需要 return。
类似于@rohit89:
VALID_CHARS = [*?a..?z, *?A..?Z, *'0'..'9']
#=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
# "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
# "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
# "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
# "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
def all_valid_chars?(str)
a = str.chars
a == a & VALID_CHARS
end
all_valid_chars?('a9Z3') #=> true
all_valid_chars?('a9 Z3') #=> false
没有正则表达式:
def validate(str)
str.count("^a-zA-Z0-9").zero? # ^ means "not"
end
上面的答案很好,但仅供参考,您的错误消息是因为您用双引号 "
启动了正则表达式。您会注意到您的方法中有奇数 (5) 个双引号。
此外,您可能希望 return true 和 false 作为值而不是引用字符串。
def alpha_numeric?(char)
if((char =~ /[[:alpha:]]) || (char =~ [[:digits:]]))
true
else
false
end
end
或
def alpha_numeric?(char)
if(char =~ /[[:alnum:]])
true
else
false
end
end
我们正在使用匹配字母和数字的正则表达式:
上面的 [[:alpha:]] ,[[:digit:]] 和 [[:alnum:]] 是 POSIX 括号表达式,它们的优点是匹配Unicode字符类别。希望这有帮助。
查看下面的 link 以获得更多选项: Ruby: How to find out if a character is a letter or a digit?
在 Ruby 2.4+.
中使用.match?
Ruby 2.4 引入了一种方便的布尔返回 .match?
方法。
对于你的情况,我会这样做:
# Checks for any characters other than letters and numbers.
# Returns true if there are none. Returns false if there are one or more.
#
def valid?( string )
!string.match?( /[^a-zA-Z0-9]/ ) # NOTE: ^ inside [] set turns it into a negated set.
end
类似于@steenslag 已经提到的非常有效的 regex-ish 方法,而且几乎一样快:
str.tr("a-zA-Z0-9", "").length.zero?
或
str.tr("a-zA-Z0-9", "") == 0
使用 tr
的一个好处是您还可以选择使用相同的基本公式分析结果:
str = "ABCxyz*123$"
rejected_chars = str.tr("a-zA-Z0-9", "")
#=> *$
is_valid = rejected_chars.length.zero?
#=> false