红宝石运算符"=〜" [英] ruby operator "=~"
本文介绍了红宝石运算符"=〜"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ruby中,我读了一些运算符,但是找不到=~
. =~
是什么意思?我看到的程序有
In ruby, I read some of the operators, but I couldn't find =~
. What is =~
for, or what does it mean? The program that I saw has
regexs = (/\d+/)
a = somestring
if a =~ regexs
我认为它正在比较somestring
是否等于数字,但是还有其他用法吗?=~
运算符的正确定义是什么?
I think it was comparing if somestring
equal to digits but, is there any other usage, and what is the proper definition of the =~
operator?
推荐答案
=~
运算符将正则表达式与字符串进行匹配,如果找到匹配项,则返回该匹配项与字符串的偏移量,否则返回nil.
The =~
operator matches the regular expression against a string, and it returns either the offset of the match from the string if it is found, otherwise nil.
/mi/ =~ "hi mike" # => 3
"hi mike" =~ /mi/ # => 3
"mike" =~ /ruby/ # => nil
您可以将字符串/正则表达式放在运算符的两侧,如上所示.
You can place the string/regex on either side of the operator as you can see above.
这篇关于红宝石运算符"=〜"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文