正则表达式在 intellij idea 中编译但不在 android studio 中并显示

regex compiling in intellij idea but not in android studio and showing

我 运行 在 java ide 中使用这个代码块,即 intellij idea 它工作正常并且还尝试了一些在线正则表达式匹配器它也在职的 但是当我 运行 在 android studio 中显示错误代码块时

public static String gettime(String temp){

    String result  = new String();

    String time=new String();
    time = ".*([0-1][0-9][:][0-5][0-9][:][0-5][0-9]).*";

    Pattern pattern = Pattern.compile(time);//error during compilation
    Matcher matcher = pattern.matcher(temp);

    if(matcher.matches()){
        result = matcher.group(1);
    }
    return result;
}

错误是

java.util.regex.PatternSyntaxException: U_ILLEGAL_ARGUMENT_ERROR
.*([0-1][0-9][:][0-5][0-9][:][0-5][0-9]).*

如果您从 [:] 切换到 :

,它会起作用
time = ".*([0-1][0-9]:[0-5][0-9]:[0-5][0-9]).*";

转义也有效:

time = ".*([0-1][0-9][\:][0-5][0-9][\:][0-5][0-9]).*";

不知道为什么 android 不喜欢 [:] 但应该和 :

一样