112

I just want to know whether ruby regex has a not match operator just like !~ in perl. I feel it's inconvenient to use (?!xxx)or (?<!xxxx) because you cannot use regex patterns in the xxx part.

0

3 Answers 3

187

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
Sign up to request clarification or add additional context in comments.

4 Comments

!~ is documented under Object.
With ruby 2.4, there's also the somewhat nicer syntax RegExp#match?, which you can easily negate. According to the release notes, it does even fewer allocations than !~
The object you're calling the method on is a String (not a Regexp), though both can receive Object's !~ method by virtue of being objects. This method compares two objects via the left operand's class' =~ method. The example in your answer has a left operand of type string and a right operand/argument of type Regexp so it calls String's =~ method; if your left operand were a regexp, it would call Regexp's =~ method.
How about in a case statement's when clause?
8

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

2 Comments

It's a lower-level solution. It's not always easy to convert regexps this way. On the other hand, this solution doesn't depend on top-level programming language ;)
Fantastic. This easily works in a case/when clause.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.