Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

Reqular Expression Negation in Ruby

| Comments

We all are aware of the pattren matching operator =~

1
"hemanth" =~ /heman/ # Does match.

But what about !=~? we don't get any errors, but boolean true value

1
2
3
4
5
6
"hemanth" !=~ /foo/ # => true
"hemanth" !=~ /bar/ # => true

#That is true always because it's like :

"hemanth".!=(~/heman/) # => != is Object class and ~ is from R.E.

So the right way :

1
2
3
# We can as well use :
"hemanth" !~ /heman/ # => flase
"hemanth" !~ /foo/ # => true

Another simple way is to use the match method ! hemanth.match("foo") #=> true

Comments