English 中文(简体)
Regex 替换所有 do does t 有效
原标题:Regex ReplaceAll Doesn t Work
  • 时间:2012-05-22 13:34:34
  •  标签:
  • java
  • android

我从另一个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 Mxfaltiples de la Cuenca del Lago de Atitlxe1n-RUMCLA (Atitlxe1n 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 Mxfaltiples de la Cuenca del Lago de Atitlxe1n-RUMCLA (Atitlxe1n Watershed Multiple Use Reserve) 

有人知道为什么这行不通吗?

最佳回答

当您在进行常规表达式匹配和替换时, 根本不应该使用 String. replaceall 方法... 您应该使用在 < a href=" http://docs. oracle.com/javase/6/docs/ api/java/ va/ util/ regex/ Matcher. html# append replace% 28java. lang. StringBuffer,% 20java. lang. string% 29" rel="no folpol"\\ code>Matser. append Replation 和 < a href=" http://docs. oracle.com/javase/6/ javatile/regex/ Matcher. html# appendendTail% 28java. l. l. tringBuffer% 29" rel=" nofol"\\colcode@ dock> Matcher. appendtail 方法如下:

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

产出:

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

replaceAll () 使用第一个参数作为regex。 在您的第一组中, 您有 xfa , 这是未逃脱的 。 请尝试在组的开头添加一个





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签