简体   繁体   中英

Regex ReplaceAll Doesn't Work

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.

The code is:

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?

You shouldn't be using String.replaceAll method at all when you are doing regular expression matching and replacement... You should be using the matchers built in Matcher.appendReplacement and Matcher.appendTail methods like this:

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);
}

Output:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM