Regex ReplaceAll no funciona

I copied this code from another StackOverflow post. However, I am having some issues with it. The items matching the pattern specified should be replaced but they are not.

El código es:

protected String FixHexValuesInString(String str){
    Log.v(TAG, "before fix: "+ str);
    Matcher matcher = Pattern.compile("\\\\x([0-9a-f]{2})").matcher(str);
    while (matcher.find()) {
        int codepoint = Integer.valueOf(matcher.group(1), 16);
        Log.v(TAG, "matcher group 0: " + matcher.group(0));
        Log.v(TAG, "matcher group 1: " + matcher.group(1));
        str = str.replaceAll(matcher.group(0), String.valueOf((char) codepoint));
    }
    Log.v(TAG, " after fix: "+ str);
    return str;
}

Here an example that I wrote to LogCat:

before fix: 'id': 1268, 'name': 'Reserva de Usos M\xfaltiples de la Cuenca del Lago de Atitl\xe1n-RUMCLA (Atitl\xe1n Watershed Multiple Use Reserve)'
matcher group 0: \xfa
matcher group 1: fa
matcher group 0: \xe1
matcher group 1: e1
matcher group 0: \xe1
matcher group 1: e1
 after fix: 'id': 1268, 'name': 'Reserva de Usos M\xfaltiples de la Cuenca del Lago de Atitl\xe1n-RUMCLA (Atitl\xe1n Watershed Multiple Use Reserve)'

Anybody see why this doesn't work?

preguntado el 22 de mayo de 12 a las 14:05

2 Respuestas

No deberías estar usando String.replaceAll method at all when you are doing regular expression matching and replacement... You should be using the matchers built in Matcher.appendReplacement y Matcher.appendTail métodos como este:

public static void main(String[] args) {

    String str = "'id': 1268, 'name': 'Reserva de Usos M\\xfaltiples de " +
                 "la Cuenca del Lago de Atitl\\xe1n-RUMCLA (Atitl\\xe1n " +
                 "Watershed Multiple Use Reserve)'";

    Matcher matcher = Pattern.compile("\\\\x([0-9a-f]{2})").matcher(str);

    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        int codepoint = Integer.valueOf(matcher.group(1), 16);
        matcher.appendReplacement(sb, String.valueOf((char) codepoint));
    }
    matcher.appendTail(sb);

    System.out.println(sb);
}

Salida:

'id': 1268, 'name': 'Reserva de Usos Múltiples de la Cuenca del Lago de Atitlán-RUMCLA (Atitlán Watershed Multiple Use Reserve)'

contestado el 22 de mayo de 12 a las 14:05

replaceAll() uses the first parameter as regex. In your first group, you have \xfa which is an unescaped \. Intente agregar un \ to the beginning of your group.

contestado el 22 de mayo de 12 a las 14:05

Why replace all occurrences with replaceAll for each match? - Dacwe

Why not? Is there a performance benefit? It looks as if he wants to replace todos los ocurrencias. - David B

"\\x4\\x61 \\x4a", Con el replaceAll method you will get the wrong result (should be \x4a J y no J J). - Dacwe

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.