java正则表达式ReplaceAll不起作用
我从另一个StackOverflow帖子上复制了这个代码。然而,我有一些问题。应替换与指定模式匹配的项目,但不替换
代码是:
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;
}
下面是我写给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)'
有人知道为什么这样不行吗
共 (0) 个答案