2017-05-30 43 views
0

我正在尤斯正則表達式在android系統來讀取用戶輸入的是這樣的: 1:10 & 3:20 D20 3:10Android的正則表達式找不到匹配

(只知道,我發送這個輸入到一個arduino)

我需要驗證用戶是否打破了線路,如果是,我發送線路到arduino。如果該行以'd'開頭(與第二個一樣),它將設置延遲,時間將成爲該行中的下一個整數;

這裏是我的代碼,但問題是,當我試圖驗證是否匹配發現我在編譯

final String expression = aux.getCodigo().toString(); 
        //System.out.println("mandeii " + MenuActivity.mIn.read()); 

        Pattern pattern = Pattern.compile("'(\n)'"); 
        Matcher matcher = pattern.matcher(expression); 
        System.out.println("achei "); 
        if (matcher.find()){ 
         System.out.println("found the first endLine"+ matcher); 
         if (matcher.group(0).charAt(0) == 'd') { 
          System.out.println("achei o d "+matcher.group(0).charAt(0)); 
          int v1 = matcher.group(0).charAt(1); 
          if (matcher.group(0).charAt(2) != '\n') { 
           int v2 = matcher.group(0).charAt(2); 
           int valor = Integer.parseInt(String.valueOf(v1) + String.valueOf(v2)); 
           System.out.println("valor "+valor); 
           new CountDownTimer(valor * 1000, 1000) { 
            public void onFinish() { 
             System.out.println("delayed"); 
            } 

            public void onTick(long millisUntilFinished) { 
             // millisUntilFinished The amount of time until finished. 
            } 
           }.start(); 

          } else { 
           new CountDownTimer(v1 * 1000, 1000) { 
            public void onFinish() { 
             System.out.println("delayed"); 
            } 

            public void onTick(long millisUntilFinished) { 
             // millisUntilFinished The amount of time until finished. 
            } 
           }.start(); 

          } 

         } 
         else MenuActivity.mOut.write(matcher.group(0).getBytes()); 

如果有人知道設置,這將幫助我很多, TKS

+0

如果要通過回車分割線'\ N'您可以用'String.split(System.lineSeparator() )' –

回答

0

所以,你只需要將行分割成塊(塊),使用\ n作爲塊分隔符。對?那麼,爲什麼做起來很複雜,因爲有一個很好的分裂方法:

// This must run in a NON-UI thread! 
    //===================================== 
    try { 
    for (String chunk: expression.split("\n")) { 
     if (chunk.charAt(0) == 'd') { 
      int valor; 
      if (chunk.length() > 1 && 
       (valor = Integer.parseInt(chunk.substring(1)))> 0) { 
        System.out.println("Delay " + valor + " millisec"); 
        Thread.sleep(valor); 
      } 
     } else 
      MenuActivity.mOut.write(chunk.getBytes()); 

     } // End of for loop 
    } catch(InterruptedException ex) { 
     System.out.println("Cancelled"); 
    } 

    } 
+0

這是完全正確的氰化物,太多的幫助。我忘了我可以使用當前的線程... tks :) – 4ury0n