简体   繁体   English

!〜方法对Ruby中的String有什么作用

[英]What does the !~ method do with String in Ruby

From @sawa's answer at: https://stackoverflow.com/a/21892359/226255 来自@ sawa的回答: https//stackoverflow.com/a/21892359/226255

What does !~ do? 什么!~做什么?

Example: 例:

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

I couldn't find any documentation in String or Regexp 我在StringRegexp中找不到任何文档

The method !~ is the inverse of =~ , that is !(=~) . 方法!~=~的倒数,即!(=~) From the Ruby Object#!~ documentation : 来自Ruby Object#!~

[obj !~ other ] returns true if two objects do not match (using the =~ method), otherwise false. 如果两个对象不匹配(使用=〜方法),则[obj!~other]返回true,否则返回false。

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. 所以,因为String#=~执行字符串/正则表达式匹配,如果匹配则返回第一个匹配的索引,否则返回nilString#!~如果匹配则返回false ,否则返回true

It means the regex does not match. 这意味着正则表达式匹配。 It's the inverse of =~ 它是=~的倒数

Also mentioned here: Does Ruby regular expression have a not match operator like "!~" in Perl? 这里还提到: Ruby正则表达式在Perl中是否有像“!〜”这样的不匹配运算符?

Apparently it's not documented for some reason. 显然,由于某种原因没有记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM