Ruby regular expression method !~

跟風遠走 提交于 2019-12-06 10:32:52

问题


I don't remember where I learned the !~ method of the String class. However I know it compares a string to a regex and check whether the string not match the regex. See my below example.

C:\>irb
irb(main):001:0> "abba" =~ /(\w)(\w)\2\1/i
=> 0
irb(main):002:0> "xxxx" =~ /(\w)(\w)\2\1/i
=> 0
irb(main):003:0> "asdf" =~ /(\w)(\w)\2\1/i
=> nil
irb(main):004:0> "asdf" !~ /(\w)(\w)\2\1/i
=> true
irb(main):005:0> "asdf" !~ /asdf/i
=> false
irb(main):006:0>

I want to find more information of the method but I can't find it in the rdoc of both String and Regexp. Anyone can give some help?

Thanks.


回答1:


Since this is the method you can find it here in the Methods filter. I've found this description.

obj !~ other → true or false

Returns true if two objects do not match (using the =~ method), otherwise false.



来源:https://stackoverflow.com/questions/14001250/ruby-regular-expression-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!