Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串替换所有GWT和OBF编译模式_Java_Regex_Gwt - Fatal编程技术网

Java 字符串替换所有GWT和OBF编译模式

Java 字符串替换所有GWT和OBF编译模式,java,regex,gwt,Java,Regex,Gwt,当我运行下面的代码时,我得到一个错误:“(TyperError)f未定义” 当我以OFB的方式说谎时,我得到了这个错误。 当我使用漂亮的风格,它是正确的工作 GWT版本:2.4 public static String replaceCommaWithDotInFloat(String text) { String result = replaceCommaWithDot(DATA_DELIMITER, text, DATA_DELIMITER); result = replac

当我运行下面的代码时,我得到一个错误:“(TyperError)f未定义” 当我以OFB的方式说谎时,我得到了这个错误。 当我使用漂亮的风格,它是正确的工作

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

对GWT 2.5.1的更新有所帮助。看起来像是GWT2.4编译器中的一个bug。

GWT2.8也没有这个问题。但是,您的代码仍然不安全(参见NHAHDH的评论)

您以
startsWith
endsWith
的形式传递文本,因此在使用构建动态正则表达式模式时需要引用这些值。当替换为这些值时,请确保转义
$
字符(如果后面跟一个数字,它将形成一个反向引用,这可能会导致异常,例如
字符串endsWith=“\\$1”
)。更好:只需捕获这些分隔符,并在替换模式中使用反向引用

使用


我不知道为什么会出现这样的错误,但将字符串直接插入正则表达式是个坏消息。尝试使用Pattern.quote在regex参数中转义startsWith和endsWith,并使用Matcher.quoteReplacement在替换字符串中转义它们。
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");
}