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

What is the difference between the two?

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.' } 
+5
source share
3 answers

In groovy, the ==~ operator (the so-called match operator) is used to match regular expressions. != - just the usual regular "not equal". So they are very different.

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

+9
source

In Java != "Not equal" but ~ "bitwise NOT". You will really do variable == ~6 .

In Groovy, the ==~ operator is "Regular Expression Matching." Examples:

  • "1234" ==~ /\d+/ → evaluates to true
  • "nonumbers" ==~ /\d+/ → evaluates to false
+6
source

In Groovy, you also need to know that in addition to ==~ , the alias "Match Operator", there is also =~ , the alias "Find Operator" and ~ , the alias "Template Operator".

Everything is explained here .

==~ Result type: Boolean / Boolean (there are no primitives in Groovy, everything is not as it seems!)

=~ Result type: java.util.regex.Matcher

~ Result Type: java.util.regex.Pattern

I assume that the Groovy interpreter / compiler can distinguish between ~ used as a Pattern operator and ~ used as bitwise NOT (that is, its use in Java) through context: the first will always be followed by a pattern that will always be enclosed in brackets in delimiters, usually.

0
source

Source: https://habr.com/ru/post/1214605/


All Articles