What does the !~ method do with String in Ruby

前端 未结 2 1058
[愿得一人]
[愿得一人] 2021-01-21 07:58

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

What does !~ do?

Example:

re = /[^\\d.,]/
\"0.0687987167581341,         


        
相关标签:
2条回答
  • 2021-01-21 08:20

    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?

    Apparently it's not documented for some reason.

    0 讨论(0)
  • 2021-01-21 08:37

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

    [obj !~ other ] returns true if two objects do not match (using the =~ method), otherwise 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.

    0 讨论(0)
提交回复
热议问题