2

In a combined regex it looks like working and it is failing when am using it in pattern. please help.

^(?=.*\\p{Nd})(?=.*\\p{L})(?!.*(.{2,})\\1).{5,12}$

this seems to be working but when I split it's failing.

^(?=.*\\p{Nd})(?=.*\\p{L})

Also I am looking for UNICODE validation to ignore any special character and just accept mixture of letters/Alpha & digits (atleast one alpha and one digit)

public void setValidations(){
        validation1 = "^(?=.*\\p{Nd})(?=.*\\p{L})"; //this is failing
        validation2 = "^.{5,12}$";
        validation3 = "(\\S+?)\\1";
        p1 = Pattern.compile(validation1);
        p3 = Pattern.compile(validation3);
    }
    public boolean validateString(String str){
        matcher1 = p1.matcher(str);
        matcher3 = p3.matcher(str);
        if(matcher1.find()){ //Expecting string passed "invalid" to fail (no numeric in it)
            System.out.println(str + " String must have letters & number at least one");
            return false;
        }
        if (!str.matches(validation2)){
            System.out.println(str  + " String must be between 5 and 12 chars in length");
            return false;
        }
        if (matcher3.find()){
            System.out.println(str + " got repeated: " + matcher3.group(1) + " String must not contain any immediate repeated sequence of characters");
            return false;    
        }
        return true;
    }
public static void main(String[] args) {
    StringValidation sv = new StringValidation();
    String s2[] = {"1newAb", "A1DOALDO", "1234567AaAaAaAa", "123456ab3434", "$1214134abA", "invalid"};
    boolean b3;
    for(int i=0; i<s2.length; i++){
       b3 = s2[i].matches("^(?=.*\\p{Nd})(?=.*\\p{L})(?!.*(.{2,})\\1).{5,12}$");

       System.out.println(s2[i] +  "  "+ b3); // string "invalid" returning false (expected)
    }
    for (String str : s2) {
            if(sv.validateString(str))
                System.out.println(str + "String is Valid");
    }
}

Also I want "$1214134abA" this string to fail since it has $

3
  • Could you please supply a set of samples along with whether you want each item to pass/fail your regex? Commented Mar 14, 2013 at 5:22
  • am passing list of strings as examples and more samples could be: VԀअs1 (valid) Vd$s1 (invalid) Vdab1 (valid) Commented Mar 14, 2013 at 5:31
  • @user1769790 your first if condition is true if a match is found! Commented Mar 14, 2013 at 5:40

2 Answers 2

2

Pattern.compile("^(?=.*\\p{Nd})(?=.*\\p{L})").matcher("invalid").find() returns false as "invalid" does not contain a digit. Thus the if condition is evaluated to false and that block is skipped.

Use ^(?=[\\p{Nd}\\p{L}]*\\p{Nd})(?=[\\p{Nd}\\p{L}]*\\p{L}) to avoid characters other than letters and digits.

It will not accept $1214134abA as it contains $.

Sign up to request clarification or add additional context in comments.

2 Comments

just tried as is if(Pattern.compile("^(?=[\\p{Nd}\\p{L}]*\\p{Nd})(?=[\\p{Nd}\\p{L}]*\\p{L})").matcher(str).find()){ System.out.println(str + " String must consist of a mixture of letters and numerical digits only, with at least one of each"); } It returns invalid string for valid strings like: A1DOALDO, 1234567AaAaAaAa, 123456ab3434
@user1769790 add negation to the if condition. now it is true if a match is found!
2

It seems that you forgot to use negation in

if(matcher1.find()){ //Expecting
    ... 
    return false;
}

It should return false if it will not find match. Try with

if(!matcher1.find()){ //Expecting...

Also since you want to check if your entire string is build on letters and digits instead of .{5,12} at the end try [\\p{L}\\p{Nd}]{5,12} .

2 Comments

that's right!! and am choosing Naveed's as answer and upvoting here.
Thank you! You know all my issues by now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.