2012-07-21 113 views
2

我在Android中使用此代碼來驗證密碼。但是現在想在BB中使用這個表達式。但它不工作,並給予例外將Android正則表達式移植到BlackBerry正則表達式不起作用

這是Android的代碼

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public static boolean isUserPassValid(String userPass) { 

     boolean isValid = false; 
     try { 
      String expression = "^.*(?=.{5,30})(?=.*\\d)(?=.*[a-zA-z]).*"; 
      CharSequence inputStr = userPass; 

      Pattern pattern = Pattern.compile(expression, 
        Pattern.CASE_INSENSITIVE); 
      Matcher matcher = pattern.matcher(inputStr); 
      if (matcher.matches()) { 
       isValid = true; 
      } 
     } catch (Exception e) { 
      Log.e(TAG, "isUserPassValid Message = " + e.toString()); 
      e.printStackTrace(); 
     } 
     return isValid; 
    } 

這是我試圖用Java來驗證現在

import com.sun.org.apache.regexp.internal.RE; 


public class Test { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     isValidPassword("Arsla"); 

    } 

    public static boolean isValidPassword(String text) { 
     System.out.println(" password for varefication is : " + text); 
     boolean isValid = false; 
     String expression = "^.*(?=.{5,30})(?=.*\\d)(?=.*[a-zA-z]).*$"; 
     String username = text; 
     RE regx = new RE(expression); 
     try { 
      isValid = regx.match(username); 
      System.out.println("password msps to the re " + isValid); 

     } catch (Exception e) { 

     } 
     return isValid; 
    } 

} 

這裏是BB代碼例外

Exception in thread "main" com.sun.org.apache.regexp.internal.RESyntaxException: Syntax error: Missing operand to closure 
    at com.sun.org.apache.regexp.internal.RECompiler.syntaxError(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.terminal(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.closure(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.branch(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.expr(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.terminal(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.closure(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.branch(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.expr(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RECompiler.compile(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RE.<init>(Unknown Source) 
    at com.sun.org.apache.regexp.internal.RE.<init>(Unknown Source) 
    at Test.isValidPassword(Test.java:19) 
    at Test.main(Test.java:10) 
+2

這兩個測試中的正則表達式模式是不同的。在BB測試中,正則表達式最後有一個'$'。我已經通過'RECompiler'的代碼進行了掃描,並且似乎有一些字符'*'導致了這個問題。 – Genzer 2012-07-21 16:12:21

+0

感謝您的回覆。你能幫忙找出正確的正則表達式嗎? – Arslan 2012-07-21 16:14:07

回答

4

也許BB正則表達式的實現不支持環顧四周。

試着在Android和黑莓手機上執行一個簡單的看看例子,看看它是否可以工作 。例如

String expression = "A(?=B)"; // only matches B if it follows an A 
+0

在BB中它給出了相同的例外。我正在嘗試關注「^ [a-zA-Z0-9 - ] {8,30} $」;它有助於獲得最小8長度的數字和後者。但不包括以確保它必須包含至少1位或1位後者。每個正則表達式實現不支持 – Arslan 2012-07-21 16:33:42

+0

四處查看。正如你現在已經證實的那樣,這是異常的來源。由於你從來沒有使用捕獲和積極的預見,這個正則表達式應該匹配相同:'「^。*(。{5,30})(。* \\ d)(。* [a-zA- z])。* $「' – 2012-07-21 19:38:44

+0

謝謝你的工作 – Arslan 2012-07-23 01:25:19