Question: I am using the following regular expression.
(?=.+[a-z])(?=.+[A-Z])(?=.+[^a-zA-Z]).{8,}

my goal is to have a password that has 3 of the 4 properties below

upper case character, lower case character, number, special character

I am using <http://rubular.com/r/i26nwsTcaU> and <http://regexlib.com/RETester.aspx> to test the expression with the following inputs

```
P@55w0rd
password1P
Password1
paSSw0rd

```

all of these should pass but only the second and fourth are passing at <http://rubular.com/r/i26nwsTcaU> and all of them pass at <http://regexlib.com/RETester.aspx>.

I also have the following code that I am using to validate

```
private void doValidate(String inputStr,String regex) {
    Pattern pattern = Pattern.compile(regex);
    if(!pattern.matcher(inputStr).matches()){
        String errMessage = "";
        throw new UniquenessConstraintViolatedException(errMessage);
    }
}

```

this code fails to validate "Password1" which should pass.
as far as the expression goes I understand it like this

```
must have lower (?=.+[a-z])
must have upper (?=.+[A-Z])
must have non alpha (?=.+[^a-zA-Z])
must be eight characters long .{8,}

```

can anyone tell me what it is I'm doing wrong.

Thanks in advance.

Answer: 
Essentially, the `.+` subexpressions are to blame, they should be `.*`. Otherwise, the lookahead part looks for lower case, upper case or non-alpha but a character of each corresponding type does not count if it is the first one in string. So, you are validating not the password, but the password with first char truncated. While @Cfreak is not right, he is close - what you are doing would not be possible with normal regex and you would have to use what he suggests. With the [lookahead](http://www.regular-expressions.info/lookaround.html) groups - `(?=)` - it is possible to do what you need. Still, personally I would rather code it like @Cfreak suggests - it is more readable and your intentions are clearer from the code. Complex regular expressions tend to be hard to write but close to impossible to read, debug, or improve after some time.
Your regex right now says you must have 1 or more lowercase characters, followed by 1 or more upper case characters followed by 1 or more upper or lowercase characters, followed by 8 characters or more. 

Regex can't do AND unless you specify where a particular character appears. You basically need to split each part of your regex into it's own regex and check each one. You can check the length with whatever string length method Java has (sorry i'm not a java dev so I don't know what it is off my head). 

Pseudo code:

```
if( regexForLower && regexForUpper && regexForSpecial && string.length == 8 ) {
     // pass
}

```