Search code examples
scalaequalityscalatest

Regex pattern equality


In ScalaTest, I have the following check:

"abc".r shouldBe "abc".r

But it is not equal. I don't understand.

abc was not equal to abc
ScalaTestFailureLocation: com.ing.cybrct.flink.clickstream.ConfigsTest$$anonfun$6 at (ConfigsTest.scala:97)
Expected :abc
Actual   :abc

Solution

  • While it's possible to decide whether two regular expressions accept the same language, it seems to be rather complicated and not all that terribly useful for everyday regex usage. Therefore, equality on compiled regex patterns is just referential equality:

    val x = "abc".r
    val y = "abc".r
    x == y
    // res0: Boolean = false