I ran Regex replaceAllIn quite some time, but ran into a problem when there was something like a regular expression in the replacement line. The problem is illustrated below (Scala 2.9.1-1). Please note that the real space of the problem is much more complicated, so the idea of ββusing a simpler solution is not really acceptable (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 results in the following error:
java.lang.IllegalArgumentException: Illegal group reference
If I changed data to something that was something simple:
val data = "This will work"
Then everything is fine.
It seems like replaceAllIn somehow looks into the second line and uses it as another RE to refer to what was remembered from the first RE ... but the docs don't say anything about it.
What am I missing?
edit : Okay, so looking at the java.util.regex.Matcher class, it seems like the supposed fix:
re.replaceAllIn(source, java.util.regex.Matcher.quoteReplacement(data))
Derek wyatt
source share