2015-04-16 27 views
0

當我運行代碼bellow時,我得到錯誤:「(TyperError)f未定義」 當我編譯OFB風格時出現此錯誤。 當我使用PRETTY風格時,它正在正常工作。字符串replaceAll GWT和OBF編譯模式

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

我不知道爲什麼你所得到的錯誤,而是直接插入串入正則表達式是個壞消息。嘗試使用Pattern.quote在正則表達式param中轉義startsWith和endsWith,並使用Matcher.quoteReplacement將它們轉義爲替換字符串。 – nhahtdh

回答

0

更新到2.5.1 GWT幫助。看起來像GWT 2.4編譯器中的一個bug。

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