簡體   English   中英

正則表達式在IntelliJ想法中編譯,但不在Android Studio中顯示

[英]regex compiling in intellij idea but not in android studio and showing

我在java ide中運行此代碼塊,即intellij的想法,它工作正常,還嘗試了一些在線正則表達式匹配器,它也可以工作,但是當我在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不喜歡[:]但是應該與:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM