I've been happily running a Regex replaceAllIn for quite a while but ran into a problem when the replacement string had something that looked like a regex in it. The following illustrates the problem (Scala 2.9.1-1). Note that the real problem space is much more complex, so the idea of using a simpler solution isn't really tenable (just to preempt the inevitable "Why don't you try ..." :D)
val data = "val re = \"\"\"^[^/]*://[^/]*/[^/]*$\"\"\".r"
val source = """here
LATEX_THING{abc}
there"""
val re = "LATEX_THING\\{abc\\}".r
println(re.replaceAllIn(source, data))
This presents with the following error:
java.lang.IllegalArgumentException: Illegal group reference
If I change data from what it was to something simple like:
val data = "This will work"
Then everything's fine.
It looks like replaceAllIn is somehow looking in the second string and using it as another RE to reference what was remembered from the first RE... but the docs say nothing about this.
What am I missing?
edit: Ok, so after looking at the java.util.regex.Matcher class, it would seem that the intended fix is:
re.replaceAllIn(source, java.util.regex.Matcher.quoteReplacement(data))