Arslan Anwar
Arslan Anwar

Reputation: 18746

Porting Android regex to BlackBerry regex not working

I am using this code in Android to validate Password. But now want to use this expression in BB. But it is not working and giving exception

This is Android Code

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

This is BB code that I am trying to validate in Java now

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

}

Here is Exception

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)

Upvotes: 2

Views: 278

Answers (1)

J&#252;rgen Steinblock
J&#252;rgen Steinblock

Reputation: 31743

Maybe the BB regex implementation does not support look arounds.

Try executing a simple look around example on both android and blackberry and see if it works. For example

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

Upvotes: 4

Related Questions