Q:

Backward slash missing from substitution in Scala

I need to construct some complex strings and for this reason I always use """(triple quotes) as delimiters for string and regexps in Scala.
In some examples, such as this:
"""foo""".r.replaceAllIn("foo", m => """backslash\andsometext""")
// result = """backslashandsometext"""
// where's the backslash?

The backslash is gone form the result.
WHat is the reason for this? Shouldn't strings wrapped in """ perform NO escaping whatsoever??

A:

You're supposed to quote your replacement string this way:
scala> import util.matching._
import util.matching._

scala> """foo""".r.replaceAllIn("foo", m => Regex quoteReplacement """backslash\andsometext""")
res2: String = backslash\andsometext

Counter-example:
scala> "f(oo)".r.replaceAllIn("foo", m => """backslash$1\$andsometext""")
res3: String = backslashoo$andsometext

So dollar means group and backslash means escape the dollar.

