I want to write a function to replace all numbers in a string with a literal \d. My code is:
val r = """\d""".r
val s = r.replaceAllIn("123abc", """\d""")
println(s)
I expect the result to be \d\d\dabc, but get:
dddabc
Then I change my code (line 2) to:
val s = r.replaceAllIn("123abc", """\\d""")
Now the result is correct: \d\d\dabc
But I don’t understand why the method replaceAllInconverts the string instead of using it directly?
In my previous code was toList, now I want. I just updated a question. Thanks to everyone.
source
share