如何检查字符串中包含ruby中的特殊字符

时间:2022-09-01 21:48:16

How to check whether a string contains special character in ruby. If I get regular expression also it is fine.

如何检查字符串是否包含ruby中的特殊字符。如果我得到正则表达式也没关系。

Please let me know

请告诉我

6 个解决方案

#1


15  

special = "?<>',?[]}{=-)(*&^%$#`~{}"
regex = /[#{special.gsub(/./){|char| "\\#{char}"}}]/

You can then use the regex to test if a string contains the special character:

然后,您可以使用正则表达式来测试字符串是否包含特殊字符:

if some_string =~ regex

This looks a bit complicated: what's going on in this bit

这看起来有点复杂:这一点发生了什么

special.gsub(/./){|char| "\\#{char}"

is to turn this

是转此

"?<>',?[]}{=-)(*&^%$#`~{}"

into this:

进入这个:

"\\?\\<\\>\\'\\,\\?\\[\\]\\}\\{\\=\\-\\)\\(\\*\\&\\^\\%\\$\\#\\`\\~\\{\\}"

Which is every character in special, escaped with a \ (which itself is escaped in the string, ie \\ not \). This is then used to build a regex like this:

哪个是特殊的字符,用\ _进行转义(它本身在字符串中转义,即\\不是\)。然后用它来构建这样的正则表达式:

/[<every character in special, escaped>]/

#2


23  

Use str.include?.

使用str.include?。

Returns true if str contains the given string or character.

如果str包含给定的字符串或字符,则返回true。

"hello".include? "lo"   #=> true
"hello".include? "ol"   #=> false
"hello".include? ?h     #=> true

#3


5  

"foobar".include?('a')
# => true

#4


3  

Why not use inverse of [:alnum:] posix.

为什么不使用[:alnum:] posix的倒数。

Here [:alnum:] includes all 0-9, a-z, A-Z.

这里[:alnum:]包括全0-9,a-z,A-Z。

Read more here.

在这里阅读更多。

#5


1  

How about this command in Ruby 2.0.0 and above?

在Ruby 2.0.0及更高版本中,这个命令怎么样?

def check_for_a_special_charachter(string)   
  /\W/ === string
end

Therefore, with:

因此,有:

!"He@llo"[/\W/].nil?  => True

!"Hello"[/\W/].nil?   => False

#6


0  

"Hel@lo".index( /[^[:alnum:]]/ )

This will return nil in case you do not have any special character and hence eaiest way I think.

如果你没有任何特殊的角色,那么这将返回零,因此我认为是最简单的方式。

#1


15  

special = "?<>',?[]}{=-)(*&^%$#`~{}"
regex = /[#{special.gsub(/./){|char| "\\#{char}"}}]/

You can then use the regex to test if a string contains the special character:

然后,您可以使用正则表达式来测试字符串是否包含特殊字符:

if some_string =~ regex

This looks a bit complicated: what's going on in this bit

这看起来有点复杂:这一点发生了什么

special.gsub(/./){|char| "\\#{char}"

is to turn this

是转此

"?<>',?[]}{=-)(*&^%$#`~{}"

into this:

进入这个:

"\\?\\<\\>\\'\\,\\?\\[\\]\\}\\{\\=\\-\\)\\(\\*\\&\\^\\%\\$\\#\\`\\~\\{\\}"

Which is every character in special, escaped with a \ (which itself is escaped in the string, ie \\ not \). This is then used to build a regex like this:

哪个是特殊的字符,用\ _进行转义(它本身在字符串中转义,即\\不是\)。然后用它来构建这样的正则表达式:

/[<every character in special, escaped>]/

#2


23  

Use str.include?.

使用str.include?。

Returns true if str contains the given string or character.

如果str包含给定的字符串或字符,则返回true。

"hello".include? "lo"   #=> true
"hello".include? "ol"   #=> false
"hello".include? ?h     #=> true

#3


5  

"foobar".include?('a')
# => true

#4


3  

Why not use inverse of [:alnum:] posix.

为什么不使用[:alnum:] posix的倒数。

Here [:alnum:] includes all 0-9, a-z, A-Z.

这里[:alnum:]包括全0-9,a-z,A-Z。

Read more here.

在这里阅读更多。

#5


1  

How about this command in Ruby 2.0.0 and above?

在Ruby 2.0.0及更高版本中,这个命令怎么样?

def check_for_a_special_charachter(string)   
  /\W/ === string
end

Therefore, with:

因此,有:

!"He@llo"[/\W/].nil?  => True

!"Hello"[/\W/].nil?   => False

#6


0  

"Hel@lo".index( /[^[:alnum:]]/ )

This will return nil in case you do not have any special character and hence eaiest way I think.

如果你没有任何特殊的角色,那么这将返回零,因此我认为是最简单的方式。