Search code examples
regexstringoperatorstilde

Difference between regular expression operators in JAPE ( Java Annotation Patterns Engine)


I would like to know the difference between these pair of regular expressions in Java Annotation Patterns Engine (JAPE).

  1. Between ==~ and =~
  2. Between !~ and !=~

As for the difference between == and =~ I have learnt that == is for complete string matching while =~ is to match a regular expression instead of a string. But when I used ==~ in place of =~ the result was the same. So kindly explain to me the difference with examples.

Thanks


Solution

  • With =~ and !~, the pattern can match any substring of the string being tested. In most regex implementations, this is the default behaviour.

    ==~ and !=~ are for whole-string matching. Typically, the same can be achieved by having the pattern start with ^ and end with $.

    Example:

    • myString =~ "[AB]" returns true if myString contains at least one A or B
    • myString ==~ "[AB]" returns true if myString is exactly "A" or "B"
    • myString !~ "[AB]" returns true if myString contains no A and no B
    • myString !=~ "[AB]" returns true for any myString that is not exactly "A" and not exactly "B"

    See also: http://gate.ac.uk/sale/tao/splitch8.html#x12-2330008.2.3