!~ 方法对 Ruby 中的 String 有什么作用 [英] What does the !~ method do with String in Ruby

查看:46
本文介绍了!~ 方法对 Ruby 中的 String 有什么作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自@sawa 的回答:https://stackoverflow.com/a/21892359/226255

From @sawa's answer at: https://stackoverflow.com/a/21892359/226255

!~ 有什么作用?

示例:

re = /[^\d.,]/
"0.0687987167581341,0.120311605902415,89.8399554017928,198.151088713489" !~ re

我在 String 中找不到任何文档或 Regexp

I couldn't find any documentation in String or Regexp

推荐答案

!~ 方法是 =~ 的逆方法,即 !(=~).来自 Ruby Object#!~ 文档:

The method !~ is the inverse of =~, that is !(=~). From the Ruby Object#!~ documentation:

[obj !~ other ] 如果两个对象不匹配(使用 =~ 方法)返回 true,否则返回 false.

[obj !~ other ] returns true if two objects do not match (using the =~ method), otherwise false.

所以,由于 String#=~ 执行字符串/正则表达式匹配,如果匹配,则返回第一个匹配项的索引,否则 nilString#!~ 返回false 如果匹配,true 否则.

So, since String#=~ performs a string/regex match returning the index of the first match if matched and nil otherwise, String#!~ return false if matched and true otherwise.

这篇关于!~ 方法对 Ruby 中的 String 有什么作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆