Does Ruby regular expression have a not match operator like "!~" in Perl?
AFAIK (?!xxx) is supported:
2.1.5 :021 > 'abc1234' =~ /^abc/
=> 0
2.1.5 :022 > 'def1234' =~ /^abc/
=> nil
2.1.5 :023 > 'abc1234' =~ /^(?!abc)/
=> nil
2.1.5 :024 > 'def1234' =~ /^(?!abc)/
=> 0
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/
=> false
irb(main):002:0> 'x' !~ /y/
=> true