Search code examples
groovysyntaxcomparisonlogical-operators

What is the difference between ==~ and != in Groovy?


What is the difference between these?

Why use one over the other?

def variable = 5
if( variable ==~ 6 && variable != 6 ) {
  return '==~ and != are not the same.'
} else {
  return '==~ and != are the same.'
}

Solution

  • In groovy, the ==~ operator (aka the "match" operator) is used for regular expression matching. != is just a plain old regular "not equals". So these are very different.

    cf. http://groovy-lang.org/operators.html