'Escape a backslash in Scala [duplicate]

I have two different strings I would like to filter out/set to none:

val str1 = Option("n/a")
val str2 = Option("N\/A")

I tried to achieve this with the following regex, by escaping the backslash character once and then matching it zero or 1 time with '?' character:

val notAvailablePattern = "[Nn]\\?/[Aa]".r

However this returns:

str1.filterNot(_.matches(notAvailablePattern.toString))
str2.filterNot(_.matches(notAvailablePattern.toString))    

Some(n/a) Some(N/A)

But this works for some reason, but I don't understand why?

val notAvailablePattern = "[Nn]\\\\?/[Aa]".r

None None

As far as I can see I'm double escaping the backslash character? While there is only one in my string.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source