String replaceAll GWT和OBF编译模式

时间:2015-04-16 10:14:11

标签: java regex gwt

当我运行代码时,我得到错误:"(TyperError)f未定义" 当我编写OFB风格时,我收到了这个错误。 当我使用PRETTY样式时,它正确地工作。

GWT版本:2.4

public static String replaceCommaWithDotInFloat(String text) {
    String result = replaceCommaWithDot(DATA_DELIMITER, text, DATA_DELIMITER);
    result = replaceCommaWithDot(LINE_DELIMITER, result, LINE_DELIMITER);
    result = replaceCommaWithDot(LINE_DELIMITER, result, DATA_DELIMITER);
    result = replaceCommaWithDot(DATA_DELIMITER, result, LINE_DELIMITER);
    return result;
}

private static String replaceCommaWithDot(String startsWith, String text, String endsWith) {
    return text.replaceAll(startsWith + "([+-]?\\d+),(\\d+)" + endsWith, startsWith + "$1.$2" + endsWith);
}

2 个答案:

答案 0 :(得分:0)

GWT 2.5.1的更新有所帮助。看起来像GWT 2.4编译器中的一个错误。

答案 1 :(得分:0)

GWT 2.8也没有问题。但是,您的代码仍然不安全(请参阅nhahtdh')

您将文字文本传递为startsWithendsWith,因此在使用RegExp#quote(java.lang.String input)构建动态正则表达式模式时,您需要引用这些值。当您使用这些值替换时,请确保转义$字符(如果后跟数字,则会形成反向引用,这可能会导致异常,例如String endsWith = "\\$1")。更好:只需捕获这些分隔符,并在替换模式中使用反向引用。

使用

private static String replaceCommaWithDot(String startsWith, String text, String endsWith) {
    return text.replaceAll("(" + RegExp.quote(startsWith) + ")([+-]?\\d+),(\\d+)(" + RegExp.quote(endsWith) + ")", "$1$2.$3$4");
}