I want to replace all occurrences of a regular expression of type "\uXXXX" where "XXXX" is an hexadecimal number representing a Unicode character to the corresponding character.
I tried the following Scala code:
def unscape(s : String) : String = {
val rex = """\\u([0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z])""".r
rex.replaceAllIn(s,m => {
hex2str(m.group(1))
}
}
def hex2str(s:String): String = {
Integer.parseInt(s,16).toChar.toString
}
If I try, for example:
unscape("Hi\\u0024, \\u0024")
it gives the following exception:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
In this other question, it seems that there could be a bug in Java's treatment of Unicode characters. Is that the problem?