Ruby:“〜〜”是什么意思?(Ruby: What does the “!~” operator mean? [duplicate])

编程入门 行业动态 更新时间:2024-10-18 22:31:16
Ruby:“〜〜”是什么意思?(Ruby: What does the “!~” operator mean? [duplicate])

这个问题在这里已经有了答案:

!〜方法在Ruby 2中对 String做了什么 回答

当声明语法如:

a !~ b

其中a,b是变量,这是什么意思?

This question already has an answer here:

What does the !~ method do with String in Ruby 2 answers

When declaring syntax such as:

a !~ b

where a,b are variables, what does it mean?

最满意答案

这是正则表达式匹配的否定。

"a" !~ /b/ # => true

当你想要检查一个字符串是否与特定模式不匹配时,这很有用。 例如,如果你想检查字符串s是否只包含数字,那么你可以这样做:

s !~ /\D/

It is negation of =~, a regex match.

"a" !~ /b/ # => true

It is useful when you want to check whether a string does not match a certain pattern. For example, if you want to check if string s includes only numbers, then you can do:

s !~ /\D/

更多推荐

本文发布于:2023-08-05 00:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Ruby   operator   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!