Does Ruby regular expression have a not match operator like "!~" in Perl?
Yes: !~
works just fine – you probably thought it wouldn’t because it’s missing from the documentation page of Regexp
. Nevertheless, it works:
irb(main):001:0> 'x' !~ /x/=> falseirb(main):002:0> 'x' !~ /y/=> true
Back in perl, 'foobar' !~ /bar/
was perfectly perlish to test that the string doesn't contain "bar".
In Ruby, particularly with a modern style guide, I think a more explicit solution is more conventional and easy to understand:
input = 'foobar'do_something unless input.match?(/bar/) needs_bar = !input.match?(/bar/)
That said, I think it would be spiffy if there was a .no_match?
method.